This code above renames all files in a set of folders to looks like "File x at x levels deep" using its current file name details, but I want it to rename the folders of these files too to "Folder x at x levels deep", but it gives an error saying that the folder is already opened in another process. The comments above is what I did until now (without success), and the rest in working as wanted.
import os, re
def everydirectory(path):
for file in os.listdir(path):
file_w_path = os.path.join(path, file)
os.chdir(path)
if os.path.isdir(file_w_path):
os.chdir(file_w_path)
# complete_folder_name = re.search("(deep)([0-9])(FOLDER)([0-9])", file)
# deepness = complete_folder_name.group(2)
# folder_number = complete_folder_name.group(4)
# new_folder_name = "Folder {} at {} levels deep".format(folder_number, deepness)
# new_folder_name_w_path = os.path.join(path, new_folder_name)
# os.rename(file_w_path, new_folder_name_w_path)
# print("Folder " + file_w_path + " renamed to " + new_folder_name_w_path)
everydirectory(file_w_path)
elif os.path.isfile(file_w_path):
path = os.path.join(path, os.getcwd())
extension = os.path.splitext(file_w_path)[1]
complete_file_name = re.search("(deep)([0-9])(FILE)([0-9])", file)
deepness = complete_file_name.group(2)
file_number = complete_file_name.group(4)
new_file_name = "File {} at {} levels deep{}".format(file_number, deepness, extension)
new_file_name_w_path = os.path.join(path, new_file_name)
os.rename(file_w_path, new_file_name_w_path)
print("File " + file_w_path + " RENAMED TO " + new_file_name_w_path)
directory = input("Where is the files?")
everydirectory(directory)
I think the problem is that it renames the file after the programs renames the folder, making it "lose" the original location of the file. Could someone check out what am I doing wrong? Thanks!
Here is the set of folders/files and how I wanted it to look after running the script: https://drive.google.com/drive/u/1/folders/0B8pLYoI76JJiOEtpNzdaYVZrVXM
I refactored your code a bit in order to make it work. I also kept track of the deep
in the recursion, but you could also rely on your filenames if you prefer:
import os, re
import os.path
from contextlib import contextmanager
@contextmanager
def change_dir(directory):
wd = os.getcwd()
try:
os.chdir(directory)
yield directory
finally:
os.chdir(wd)
def rename_file(filename, deep, is_dir=False):
if is_dir:
complete_filename = re.search("(deep)([0-9])(FOLDER)([0-9])", filename)
file_number = complete_filename.group(4)
new_filename = "Folder {} at {} levels deep".format(file_number, deep)
else:
complete_filename = re.search("(deep)([0-9])(FILE)([0-9])", filename)
file_number = complete_filename.group(4)
extension = os.path.splitext(filename)[1]
new_filename = "File {} at {} levels deep{}".format(file_number, deep, extension)
os.rename(filename, new_filename)
def every_directory(path, deep=0):
deep += 1
for file in os.listdir(path):
is_dir = False
filepath = os.path.join(path, file)
if os.path.isdir(filepath):
every_directory(filepath, deep)
is_dir = True
with change_dir(path):
rename_file(file, deep=deep, is_dir=is_dir)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.