Reputation: 17
I'm trying to copy every single csv file within a source directory and it's sub-folders into a new "mega" folder. The end result would be a folder containing nothing except the csv files found within the source directory.
The problem I'm faced with is that some of the csv file names are the same. Therefore when copying the files, the files with the same names are being overwritten. I want to be able to rename them, instead of overwriting them. An example of the format I'd like to rename the files is:
I've found this thread, but the answers didn't work for me.
My code is as follows (based on the link provided):
movdir = r"Source Directory"
basedir = r"Destination Folder"
# Walk through all files in the directory that contains the files to copy
for root, dirs, files in os.walk(movdir):
for filename in files:
# I use absolute path, case you want to move several dirs.
old_name = os.path.join(os.path.abspath(root), filename)
# Separate base from extension
base, extension = os.path.splitext(filename)
# Initial new name
new_name = os.path.join(basedir, base, filename)
# If folder basedir/base does not exist... You don't want to create it?
if not os.path.exists(os.path.join(basedir, base)):
print(os.path.join(basedir,base), "not found")
continue # Next filename
elif not os.path.exists(new_name): # folder exists, file does not
shutil.copy(old_name, new_name)
else: # folder exists, file exists as well
ii = 1
while True:
new_name = os.path.join(basedir,base, base + "_" + str(ii) + extension)
if not os.path.exists(new_name):
shutil.copy(old_name, new_name)
print("Copied", old_name, "as", new_name)
break
ii += 1
When I run this code, it just prints that every single csv file within the source directory is "not found", and none of the files are copied at all.
Any help or information on this would be greatly appreciated.
Upvotes: 1
Views: 185
Reputation: 1300
try the following modifications:
movedir = r"source"
basedir = r"destination"
# Walk through all files in the directory that contains the files to copy
for root, dirs, files in os.walk(movdir):
for filename in files:
# I use absolute path, case you want to move several dirs.
old_name = os.path.join(os.path.abspath(root), filename)
file_path,file_bare_name = os.path.split(filename) # this is were ur code didn't work as u use base as the bare file name and the relative path to source ambiguously.
base, extension = os.path.splitext(file_bare_name)
file_relative_path_to_source = root[len(movedir)+1:] #removing the old dir name from the relative path
if extension=='.csv': # taking only csv files
# Initial new name
new_name = os.path.join(basedir, file_bare_name)
if not os.path.exists(new_name): # file dosn't exist
shutil.copy(old_name, new_name)
else: # copies being renamed
ii = 1
while True:
new_name = os.path.join(basedir, file_bare_name + "_" + str(ii) + extension)
if not os.path.exists(new_name):
shutil.copy(old_name, new_name)
print("Copied", old_name, "as", new_name)
break
ii += 1
Upvotes: 1