简体   繁体   中英

Python: Changing filenames and folder names in all folders and subfolders

I want to change the file names and folder names in a given directory and all subfolders. My folder structure is as follows:

  • top directory
    • file1
    • file2
    • folder1
      • file1
      • file2
      • file3
      • file4
    • folder2
      • file1
      • file2
      • file3
      • file4

I get the following error when executing the code below. I already checked the forums, but couldn't find a solution. Could someone please help me solve this issue and let me know what I need to do to get the program working? Or is there a better solution for renaming files and folders in a tree?

Error message

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'Filename 1' -> 'filename_1'

Code

#Change file names and folder names in a given directory and all its 
subfolders

import os

os.chdir("path\\to\\folder")
print(os.getcwd())

#Walk the directory and change the file names and folder names in all folders and subfolders.

for root, dirs, files in os.walk("path\\to\\folder"):
    for dir_name in dirs:
        os.rename(dir_name, dir_name.replace(" ", "_").lower())


    for file_name in files:
        os.rename(file_name, file_name.replace(" ", "_").lower())



#Print done once all files have been renamed.      
print("done")

could it be that you're walking on a folder while renaming it so that it can't be found?

looks like you first need to rename the files and only then the dirs (and even then - make sure it's bottom up)

尝试先更改文件名,否则将更改dir_name并丢失引用。

You need to use root otherwise the rename can't find the path:

for root, dirs, files in os.walk("path/to/folder"):
    for name in dirs + files:
        os.rename(os.path.join(root, name), os.path.join(root, name.replace(" ", "_").lower()))

Following solution works most of the time, still issues like same name files may exist after normalizing the name.

import os

os.chdir("path/to/dir")
print(os.getcwd())

#Walk the directory and change the file names and folder names in all folders and subfolders.

for root, dirs, files in os.walk("path/to/dir", topdown=False):
   for file_name in files:
      new_name = file_name.replace(" ", "_").lower()
      if (new_name != file_name):
         os.rename(os.path.join(root, file_name), os.path.join(root, new_name))

   for dir_name in dirs:
      new_name = dir_name.replace(" ", "_").lower()
      if (new_name != dir_name):
         os.rename(os.path.join(root, dir_name), os.path.join(root, new_name))

Here I copied all files inside of each subfolder to another path. First use os.listdir() to move through each folder, then use it to move through files which are inside of the folder path. Finally use os.rename() to rename file name. Here, I changed file name to folder name_file name which is "folder_file":

path = 'E:/Data1'
path1 = 'E:/Data2'
for folder in os.listdir(path):
    for file in os.listdir(path + '/'+ folder):
        src = path + '/' + folder + '/' + file
        des = path1 + '/' +  folder + '_' +file
        os.rename(src, des)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM