0

I am trying to rename the most recent folder in a directory but getting the following error:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Program Files\\Ampps\\www\\DAT_Simulation\\Practice' -> 'C:\\Program Files\\Ampps\\www\\DAT_Simulation\\Practice\\lolol'

My code is the following:

import os
dirs = [d for d in os.listdir(".") if os.path.isdir(d)]
a = sorted(dirs, key=lambda x: os.path.getctime(x), reverse=True)[:1]
current_name = ''.join(a)

path = r"C:\Program Files\Ampps\www\DAT_Simulation\Practice"
b = os.path.join(path, current_name)

for root, dirs, files in os.walk(path):
    for directory in dirs:
        # Apply input argument here
       new_name = 'lolol'
       new_path = path + "\\"+new_name
       os.rename(path, new_path)

Got my help from the following links: Renaming a directory in Python How to get the newest directory in Python

EDIT: As stated in the comments the loop was not necessary. And I should't be using path at the end.

new_name = 'lolol'
new_path = path + "\\"+new_name
os.rename(b, new_path)
ian305
  • 25
  • 6

0 Answers0