Skip to content Skip to sidebar Skip to footer

Renaming A Directory In Python

I'm trying to rename a directory in Python, such hat the directory will be renamed to its first 8 characters of its original name. This is what I did: import os path = '/home/dire

Solution 1:

You need to specify full path of directory

import ospath = 'C:\\Users\\demo_test_eg'for root, dirs, files inos.walk(path):
    for directory in dirs:
       new_name = directory[0:8]
       path1 = path + "\\"+ directory#Full path of directory
       new_path = path + "\\"+new_name#Full path of file whose name is changed
       os.rename(path1, new_path)

Note :

I have added "\\" for windows

Post a Comment for "Renaming A Directory In Python"