如何在多个子目录中使用python向文件添加扩展名
我需要添加一个.jpg扩展到大约300K图片。它们都在12个子目录中,在这12个子目录中还有4个子目录 我试着跟随这篇文章,但没有走到所有的子目录: 我还尝试了以下方法:如何在多个子目录中使用python向文件添加扩展名,python,python-3.x,Python,Python 3.x,我需要添加一个.jpg扩展到大约300K图片。它们都在12个子目录中,在这12个子目录中还有4个子目录 我试着跟随这篇文章,但没有走到所有的子目录: 我还尝试了以下方法: 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)
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)
上面的代码运行,但什么也没发生
上面的代码运行,但什么也没发生
我怀疑有两个问题:
应该是os.splitext
os.path.splitext
不应指定os.path.join
,因此路径名
应该是os.path.join(path, pathnames, file)
及 应该是os.path.join(path, file)
os.path.join(path, file + '.jpg')
os
,我会感到惊讶。
os.path.join(path, file + '.jpg')