SystemError
SystemError

Reputation: 11

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:

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")

Upvotes: 1

Views: 2848

Answers (5)

Moon
Moon

Reputation: 1

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)

Upvotes: 0

jbp
jbp

Reputation: 1621

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))

Upvotes: 0

Maxime Chéramy
Maxime Chéramy

Reputation: 18851

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()))

Upvotes: 1

forayer
forayer

Reputation: 367

Try to change the filename first, otherwise you'll change the dir_name and lose reference.

Upvotes: 0

akiva
akiva

Reputation: 2737

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)

Upvotes: 0

Related Questions