I need to create a copy of a directory tree called "caritemscopy" where all files, instead of being in directories named after years, rather have the year as part of the filename, and the year directories are entirely absent
My directory currently looks like this
After coding my directory should looks like this
It will list all directory files in path, then rename it and save all files at path and remove the empty directory.
import os
path = 'path_of_folder/F26/'
files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
for file in f:
if '.txt' in file:
files.append(os.path.join(r, file))
for f in files:
src = f.split('/')
os.rename(f, path + src[-2]+'-'+src[-1])
if not os.listdir(path+src[-2]):
os.rmdir(path+src[-2])
else:
pass
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.