![](/img/trans.png)
[英]How to rename all files names in folder based on another file name folder?
[英]how to return all file names from 1 folder, if files match to another folder?
变量
first= 'C:/Users/shady/Desktop/test folder/check'
second = 'C:/Users/shady/Desktop/test folder/checked'
里面first
有一个文件名,名为
test 1 ed.docx
&
在second
里面有一个类似的文件名,扩展名不同,还有一个额外的文件:
test 1.jpg
test 2.jpg
问题
通过查看我的代码,我需要设置choices
以从second
返回所有文件,但如果两个文件夹具有相似的文件名(不包括扩展名但包括ed
),则不要显示该文件名。
我当前的代码
import os
import glob
first= 'C:/Users/shady/Desktop/test folder/check'
second = 'C:/Users/shady/Desktop/test folder/checked'
choices = os.listdir(second)
print(choices)
当前 output
['test 1.jpg', 'test 2.jpg']
所需 output
['test 2.jpg']
这是 output,因为文件名 ( test 2 ed.docx
) 里面不存在first
。
和
test 1.jpg
存在于first
因为test 1 ed.docx
是一个很好的匹配。
这就是我要去的地方:
import os
Username = "ed"
first = 'C:/Users/shady/Desktop/test folder/check/'
second = 'C:/Users/shady/Desktop/test folder/checked/'
choices = os.listdir(second)
list = []
for x in choices:
x = (x.strip('.JPG') + ' ' + Username + '.docx')
但不确定这是否是我使用此代码的正确方向..
尝试这个:
import os
Username = "ed"
first = 'C:/Users/shady/Desktop/test folder/check/'
second = 'C:/Users/shady/Desktop/test folder/checked/'
one = [f for f in os.listdir(first) if isfile(join(first, f))]
two = [f for f in os.listdir(second) if isfile(join(second, f))]
for x in [one]:
x1 = [y.rstrip(' ' + Username + '.docx')+'.jpg' for y in x]
choices = (set(two).difference(x1)) # returns all files that are not the same based on 'x1'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.