[英]How do I add extensions to files using python in multiple subdirectories
I need to add a .jpg extension to around 300K pictures. 我需要为30万张图片添加.jpg扩展名。 They are all in 12 sub-directories and four more subdirectories in each of those 12.
它们全部位于12个子目录中,并且在这12个子目录中又有四个子目录。
I tried following this post but didn't do a walk down to all subdirectories: Adding extension to multiple files (Python3.5) 我尝试了这篇文章,但没有深入所有子目录: 向多个文件添加扩展名(Python3.5)
I also tried the following: 我还尝试了以下方法:
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. 以上运行,但没有任何反应。
The above runs but nothing happens.
以上运行,但没有任何反应。
I doubt that, there are 2 problems: 我怀疑有两个问题:
os.splitext
should be os.path.splitext
os.splitext
应该是os.path.splitext
os.path.join
should not be given pathnames
, so os.path.join
不应该被赋予pathnames
,所以
os.path.join(path, pathnames, file)
should be 应该
os.path.join(path, file)
and 和
os.path.join(path, pathnames, file + '.jpg')
should be 应该
os.path.join(path, file + '.jpg')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.