我需要为大约 300K 张图片添加 .jpg 扩展名。它们全部位于 12 个子目录中,并且这 12 个子目录中的每一个子目录中还有 4 个子目录。
我尝试按照这篇文章进行操作,但没有深入到所有子目录: Adding extension to multiple files (Python3.5)
我还尝试了以下方法:
import os
path = 'C:\\Photos'
genmod = os.walk(path)
for path, pathnames, files in gen_obj:
for file in files:
head, tail = os.splitext(file)
if not tail:
src = os.path.join(path, pathnames, file)
dst = os.path.join(path, pathnames, file + '.jpg')
if not os.path.exists(dst): # check if the file doesn't exist
os.rename(src, dst)
以上运行但没有任何反应。
最佳答案
The above runs but nothing happens.
我怀疑,有两个问题:
os.splitext
应为os.path.splitext
os.path.join
不应给出路径名
,因此os.path.join(path, pathnames, file)
应该是
os.path.join(path, file)
和
os.path.join(path, pathnames, file + '.jpg')
应该是
os.path.join(path, file + '.jpg')
关于python - 如何使用 python 在多个子目录中为文件添加扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57999039/