3 回答

TA贡献1790条经验 获得超9个赞
只记得我曾经写过几个文件来解决这个确切的问题。你可以在我的 Github 上找到源代码。
简而言之,这里有两个有趣的函数:
list_files(loc, return_dirs=False, return_files=True, recursive=False, valid_exts=None)
copy_files(loc, dest, rename=False)
对于您的情况,您可以将这些函数复制并粘贴到您的项目中并copy_files
像这样修改:
def copy_files(loc, dest, rename=False):
# get files with full path
files = list_files(loc, return_dirs=False, return_files=True, recursive=True, valid_exts=('.txt',))
# copy files in list to dest
for i, this_file in enumerate(files):
# change name if renaming
if rename:
# replace slashes with hyphens to preserve unique name
out_file = sub(r'^./', '', this_file)
out_file = sub(r'\\|/', '-', out_file)
out_file = join(dest, out_file)
copy(this_file, out_file)
files[i] = out_file
else:
copy(this_file, dest)
return files
然后就这样称呼它:
copy_files('mailDir', 'destFolder', rename=True)
重命名方案可能不是您想要的,但至少不会覆盖您的文件。我相信这应该可以解决您的所有问题。

TA贡献1851条经验 获得超5个赞
干得好:
import os
from os import path
import shutil
destDir = '<absolute-path>'
for root, dirs, files in os.walk(os.getcwd()):
# Filter out only '.txt' files.
files = [f for f in files if f.endswith('.txt')]
# Filter out only 'inbox' directory.
dirs[:] = [d for d in dirs if d == 'inbox']
for f in files:
p = path.join(root, f)
# print p
shutil.copy(p, destDir)
快速而简单。
抱歉,我忘记了其中的部分,您还需要唯一的文件名。上述解决方案仅适用于单个收件箱文件夹中的不同文件名。
要从多个收件箱复制文件并在目标文件夹中具有唯一名称,您可以尝试以下操作:
import os
from os import path
import shutil
sourceDir = os.getcwd()
fixedLength = len(sourceDir)
destDir = '<absolute-path>'
filteredFiles = []
for root, dirs, files in os.walk(sourceDir):
# Filter out only '.txt' files in all the inbox directories.
if root.endswith('inbox'):
# here I am joining the file name to the full path while filtering txt files
files = [path.join(root, f) for f in files if f.endswith('.txt')]
# add the filtered files to the main list
filteredFiles.extend(files)
# making a tuple of file path and file name
filteredFiles = [(f, f[fixedLength+1:].replace('/', '-')) for f in filteredFiles]
for (f, n) in filteredFiles:
print 'copying file...', f
# copying from the path to the dest directory with specific name
shutil.copy(f, path.join(destDir, n))
print 'copied', str(len(filteredFiles)), 'files to', destDir
如果您需要复制所有文件而不仅仅是 txt 文件,则只需将条件更改f.endswith('.txt')为os.path.isfile(f)同时过滤掉文件即可。
添加回答
举报