简体   繁体   English

Python:更改所有文件夹和子文件夹中的文件名和文件夹名

[英]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文件 1
    • file2文件 2
    • folder1文件夹 1
      • file1文件 1
      • file2文件 2
      • file3文件 3
      • file4文件 4
    • folder2文件夹 2
      • file1文件 1
      • file2文件 2
      • file3文件 3
      • file4文件 4

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' FileNotFoundError: [WinError 2] 系统找不到指定的文件:'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: 您需要使用root否则重命名找不到路径:

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.首先使用os.listdir()在每个文件夹中移动,然后使用它在文件夹路径内的文件中移动。 Finally use os.rename() to rename file name.最后使用os.rename()重命名文件名。 Here, I changed file name to folder name_file name which is "folder_file":在这里,我将文件名更改为文件夹 name_file 名称,即“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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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