将目录中的所有文件复制到新目录,同时重命名 Python 中具有相同名称的文件
Copy all files within directory into new directory while renaming files with same name in Python
我正在尝试将源目录及其子文件夹中的每个 csv 文件复制到新的“mega”文件夹中。最终结果将是一个文件夹,除了在源目录中找到的 csv 文件外,什么也没有。
我遇到的问题是某些 csv 文件名相同。因此在复制文件时,会覆盖同名文件。我希望能够重命名它们,而不是覆盖它们。我想重命名文件的格式示例是:
- abcd
- abcd_1
- abcd_2,等等...
我找到了 this thread,但答案对我不起作用。
我的代码如下(基于link提供):
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
当我 运行 这段代码时,它只是打印出源目录中的每个 csv 文件都“未找到”,并且 none 的文件被完全复制了。
如有任何帮助或信息,我们将不胜感激。
尝试以下修改:
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
我正在尝试将源目录及其子文件夹中的每个 csv 文件复制到新的“mega”文件夹中。最终结果将是一个文件夹,除了在源目录中找到的 csv 文件外,什么也没有。
我遇到的问题是某些 csv 文件名相同。因此在复制文件时,会覆盖同名文件。我希望能够重命名它们,而不是覆盖它们。我想重命名文件的格式示例是:
- abcd
- abcd_1
- abcd_2,等等...
我找到了 this thread,但答案对我不起作用。
我的代码如下(基于link提供):
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
当我 运行 这段代码时,它只是打印出源目录中的每个 csv 文件都“未找到”,并且 none 的文件被完全复制了。
如有任何帮助或信息,我们将不胜感激。
尝试以下修改:
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