Python 递归重命名文件扩展名

Python 递归重命名文件扩展名,python,Python,我很难创建一个python脚本来重命名文件夹中的文件扩展名,并在子目录中继续这样做。这是我迄今为止的剧本;它只能重命名顶级目录中的文件: #!/usr/bin/python # Usage: python rename_file_extensions.py import os import sys for filename in os.listdir ("C:\\Users\\username\\Desktop\\test\\"): # parse through file list in

我很难创建一个python脚本来重命名文件夹中的文件扩展名,并在子目录中继续这样做。这是我迄今为止的剧本;它只能重命名顶级目录中的文件:

#!/usr/bin/python
# Usage: python rename_file_extensions.py

import os
import sys

for filename in os.listdir ("C:\\Users\\username\\Desktop\\test\\"): # parse through file list in the folder "test"

    if filename.find(".jpg") > 0: # if an .jpg is found

            newfilename = filename.replace(".jpg","jpeg") # convert .jpg to jpeg

            os.rename(filename, newfilename) # rename the file

您可以按如下方式处理目录:

import os

def process_directory(root):

    for item in os.listdir(root):
        if os.path.isdir(item):
            print("is directory", item)
            process_directory(item)
        else:
            print(item)
            #Do stuff

process_directory(os.getcwd())
for subdir, dirs, files in os.walk(root):
    for f in files:
        if f.find('.jpg') > 0:
            #The rest of your stuff
尽管如此,这并不是真的必要。只需使用
os.walk
,它将遍历所有顶级目录和其他目录/文件

如下操作:

import os

def process_directory(root):

    for item in os.listdir(root):
        if os.path.isdir(item):
            print("is directory", item)
            process_directory(item)
        else:
            print(item)
            #Do stuff

process_directory(os.getcwd())
for subdir, dirs, files in os.walk(root):
    for f in files:
        if f.find('.jpg') > 0:
            #The rest of your stuff
这正是你想要的

import os
import sys

directory = os.path.dirname(os.path.realpath(sys.argv[0])) #get the directory of your script
for subdir, dirs, files in os.walk(directory):
 for filename in files:
  if filename.find('.jpg') > 0:
   subdirectoryPath = os.path.relpath(subdir, directory) #get the path to your subdirectory
   filePath = os.path.join(subdirectoryPath, filename) #get the path to your file
   newFilePath = filePath.replace(".jpg",".jpeg") #create the new name
   os.rename(filePath, newFilePath) #rename your file

我用文件路径修改了Jaron的答案,重命名文件的完整示例

我稍微修改了Hector Rodriguez Jr.的答案,因为它将替换路径中出现的任何
“.jpg”
,例如
/path/to/my.jpg.files/001.jpg
将变成
/path/to/my.jpg.files/001.jpeg>,这不是你想要的,对吧

虽然在文件夹名称中使用点通常不是一个好主意,但它也可能发生

import os
import sys

directory = os.path.dirname(os.path.realpath(sys.argv[0])) # directory of your script
for subdir, dirs, files in os.walk(directory):
    for filename in files:
        if filename.find('.jpg') > 0:
            newFilename = filename.replace(".jpg", ".jpeg") # replace only in filename
            subdirectoryPath = os.path.relpath(subdir, directory) # path to subdirectory
            filePath = os.path.join(subdirectoryPath, filename) # path to file
            newFilePath = os.path.join(subdirectoryPath, newFilename) # new path
            os.rename(filePath, newFilePath) # rename

这可能是重复的正是我要找的!将它放在目录的根目录中并运行它。它将为根目录和子目录中的所有内容更改相应的扩展名!令人惊叹的!os.getcwd()可以替换os.path.dirname(os.path.realpath(sys.argv[0]))