Home > Blockchain >  Rename folders using pandas
Rename folders using pandas

Time:05-12

A folder named Folder have several sub folder Sub Folder A, Sub Folder B and Sub Folder C

I want to make them sub folder a, sub folder b and sub folder c

Is there any way to that?

CodePudding user response:

Not sure there is a function in pandas to do that. However the method os.rename() should do the trick when used in combination with os.walk(directory):

CodePudding user response:

I think you don't need pandas here, you can rename with os.rename(folder, newname).

import os

os.rename(folder, new_name)

You need the exact path to the folder, for example, 'Folder/FolderA'

CodePudding user response:

Alternatively you can use shutil.move(old_path, new_path).

import shutil

old_path = r"C:\Users\adj\Desktop\root_dir\Sub Folder A"
new_path = r"C:\Users\adj\Desktop\root_dir\sub folder a"

shutil.move(old_path, new_path)

But based on your example, maybe you just want to lower case the folders name? Then you can use os.rename() and the lower() method

import os

root_folder = r"C:\Users\adj\Desktop\root_dir"
for f in os.listdir(root_folder):
    f_path = os.path.join(root_folder, f)
    os.rename(f_path, f_path.lower())
  • Related