用python遍历文件夹1,得到所有pdf文件的名称。遍历文件夹2中的所有txt文件名称,如果pdf文件的名称没有对应同名的txt文件名称,就在控制台输出pdf文件的所在路径,反之不做任何操作。
时间: 2023-05-28 12:06:35 浏览: 55
可以使用os模块和glob模块来实现。
```python
import os
import glob
# 遍历文件夹1中所有pdf文件的名称
pdf_files = glob.glob('文件夹1/*.pdf')
pdf_names = [os.path.basename(file) for file in pdf_files]
# 遍历文件夹2中所有txt文件名称
txt_files = glob.glob('文件夹2/*.txt')
txt_names = [os.path.basename(file) for file in txt_files]
# 判断pdf文件是否有对应同名的txt文件
for pdf_name in pdf_names:
if pdf_name[:-4] + '.txt' not in txt_names:
pdf_path = os.path.join('文件夹1', pdf_name)
print(pdf_path)
```
首先使用glob模块得到文件夹1中所有pdf文件的路径,然后通过os模块获取文件名并保存在pdf_names列表中。
接着使用glob模块得到文件夹2中所有txt文件的路径,同样通过os模块获取文件名并保存在txt_names列表中。
最后遍历pdf_names列表中的每个pdf文件名,判断其是否有对应同名的txt文件名,如果没有,则输出pdf文件的路径。
相关问题
用python遍历文件夹1,得到所有pdf文件的名称。遍历文件夹2中的所有txt文件名称,如果pdf文件的名称没有对应同名的txt文件名称,就在控制台输出pdf文件名称,反之不做任何操作。
以下是示例代码:
```python
import os
# 遍历文件夹1,得到所有pdf文件的名称
pdf_files = []
for root, dirs, files in os.walk('folder1'):
for file in files:
if file.endswith('.pdf'):
pdf_files.append(file)
# 遍历文件夹2,如果pdf文件的名称没有对应同名的txt文件名称,就在控制台输出pdf文件名称
for root, dirs, files in os.walk('folder2'):
for file in files:
if file.endswith('.txt'):
pdf_name = os.path.splitext(file)[0] + '.pdf'
if pdf_name in pdf_files:
pdf_files.remove(pdf_name)
else:
continue
# 输出没有对应txt文件的pdf文件名称
for pdf_file in pdf_files:
print(pdf_file)
```
首先,我们使用os.walk()函数遍历文件夹1,得到所有pdf文件的名称,并保存在pdf_files列表中。然后,使用os.walk()函数遍历文件夹2,对于每一个txt文件,判断其是否有对应同名的pdf文件。如果有,就将该pdf文件的名称从pdf_files列表中移除。最后,输出没有对应txt文件的pdf文件名称。
用python遍历文件夹1,得到所有pdf文件的名称。遍历文件夹2中的所有txt文件名称,如果pdf文件的名称无法被任何txt文件名称包含,就在控制台输出pdf文件的所在路径,反之不做任何操作。
可以使用os模块中的walk()函数遍历文件夹,使用正则表达式来匹配文件名。代码如下:
```python
import os
import re
# 遍历文件夹1,找到所有pdf文件
pdf_files = []
for root, dirs, files in os.walk("folder1"):
for file in files:
if re.search(r'\.pdf$', file):
pdf_files.append(os.path.join(root, file))
# 遍历文件夹2,找到所有txt文件
txt_files = []
for root, dirs, files in os.walk("folder2"):
for file in files:
if re.search(r'\.txt$', file):
txt_files.append(file)
# 判断pdf文件名是否被txt文件名包含
for pdf_file in pdf_files:
flag = False
for txt_file in txt_files:
if txt_file.find(os.path.basename(pdf_file)) != -1:
flag = True
break
if not flag:
print(pdf_file)
```
首先,使用两次for循环分别遍历文件夹1和文件夹2中的所有文件。使用正则表达式来匹配文件名,只保留后缀为pdf或txt的文件。pdf文件的完整路径保存在列表pdf_files中,txt文件的文件名保存在列表txt_files中。
接着,对于每个pdf文件,使用一个flag变量来记录是否被txt文件名包含。遍历txt_files列表中的每个txt文件,判断pdf文件名是否被txt文件名包含。如果被包含,则将flag标记为True,跳出循环。如果在txt_files中没有找到任何一个文件名包含pdf文件名,则说明pdf文件名无法被任何txt文件名包含,输出pdf文件的完整路径。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)