两个文件夹内名字相同的文件
时间: 2023-08-07 21:03:59 浏览: 74
您可以使用以下命令行代码来列出两个文件夹内名字相同的文件:
```bash
diff <(cd folder1 && ls) <(cd folder2 && ls) | grep "^>"
```
这个命令会首先列出两个文件夹中的所有文件,然后使用 diff 命令比较它们。最后,使用 grep 命令找出只属于第二个文件夹的文件,也就是名字相同的文件。
相关问题
判断两个文件夹中文件名字后5个字符是否相同
可以使用Python的os模块来遍历文件夹中的文件,并使用字符串切片来比较文件名字后5个字符是否相同。以下是一个示例代码:
```python
import os
folder1 = "/path/to/folder1"
folder2 = "/path/to/folder2"
# 获取文件夹中的所有文件名
files1 = os.listdir(folder1)
files2 = os.listdir(folder2)
# 遍历文件夹1中的所有文件,并比较文件名字后5个字符是否与文件夹2中的对应文件相同
for file1 in files1:
if file1[-5:] == files2[files1.index(file1)][-5:]:
print(f"文件 {file1} 的后5个字符与文件夹2中的对应文件相同。")
else:
print(f"文件 {file1} 的后5个字符与文件夹2中的对应文件不同。")
```
在上述代码中,我们使用os.listdir()函数获取文件夹中的所有文件名,然后在遍历文件夹1中的所有文件时,使用文件名字后5个字符与文件夹2中对应文件名字的后5个字符进行比较。如果相同,则输出相应的提示信息。
用python写一份两个文件夹相同名字图片对比的脚本
好的,以下是一个用 Python 写的比较两个文件夹中同名图片的脚本:
```python
import os
from PIL import Image
folder1 = './folder1'
folder2 = './folder2'
for filename in os.listdir(folder1):
if filename.endswith('.jpg') or filename.endswith('.jpeg') or filename.endswith('.png'):
if os.path.isfile(os.path.join(folder2, filename)):
img1 = Image.open(os.path.join(folder1, filename))
img2 = Image.open(os.path.join(folder2, filename))
if img1.size == img2.size and img1.mode == img2.mode:
diff = ImageChops.difference(img1, img2)
if diff.getbbox() is None:
print(f'{filename} is identical in both folders')
else:
print(f'{filename} is different in both folders')
else:
print(f'{filename} is different in both folders')
else:
print(f'{filename} does not exist in {folder2}')
```
该脚本会遍历 folder1 文件夹中的所有图片文件,判断其是否也存在于 folder2 文件夹中,如果存在,则会用 PIL 库中的 Image 对象来比较两张图片是否相同,最后输出结果。需要注意的是,该脚本默认两个文件夹中的图片文件名相同。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)