支持通配符的 Python Windows 文件复制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2584414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Windows File Copy with Wildcard Support
提问by Wang Dingwei
I've been doing this all the time:
我一直在这样做:
result = subprocess.call(['copy', '123*.xml', 'out_folder\.', '/y'])
if result == 0:
do_something()
else:
do_something_else()
Until today I started to look into pywin32 modules, then I saw functions like win32file.CopyFiles(), but then I found it may not support copying files to a directory. Maybe this functionality is hidden somewhere, but I haven't found it yet.
直到今天我才开始研究pywin32模块,然后我看到了win32file.CopyFiles()之类的功能,但后来发现它可能不支持将文件复制到目录。也许这个功能隐藏在某个地方,但我还没有找到。
I've also tried "glob" and "shutil" combination, but "glob" is incredibly slow if there are many files.
我也尝试过“glob”和“shutil”组合,但是如果有很多文件,“glob”会非常慢。
So, how do you emulate this Windows command with Python?
那么,你如何用 Python 模拟这个 Windows 命令呢?
copy 123*.xml out_folder\. /y
回答by Frederik
The following code provides a portable implementation.
以下代码提供了可移植的实现。
Note that I'm using iglob (added in Python 2.5) which creates a generator, so it does not load the entire list of files in memory first (which is what glob does).
请注意,我正在使用创建生成器的 iglob(在 Python 2.5 中添加),因此它不会首先将整个文件列表加载到内存中(这正是 glob 所做的)。
from glob import iglob
from shutil import copy
from os.path import join
def copy_files(src_glob, dst_folder):
for fname in iglob(src_glob):
copy(fname, join(dst_folder, fname))
if __name__=='__main__':
copy_files("123*.xml", "out_folder")
Additional documentation:
附加文件:
回答by cryo
The below example is fairly naive - doesn't do any checking if something goes wrong, and doesn't create any directories, but might do what you want:
下面的例子相当天真——不检查是否有问题,也不创建任何目录,但可能会做你想做的事:
import glob
import shutil
for path in glob.iglob('123*.xml'):
shutil.copy(path, 'out_folder/%s' % path)
See also: http://timgolden.me.uk/python/win32_how_do_i/copy-a-file.html
另见:http: //timgolden.me.uk/python/win32_how_do_i/copy-a-file.html
Using win32file/SHFileOperation copy seems to be more functional, but aren't as portable and are more complicated to use.
使用 win32file/SHFileOperation 副本似乎更实用,但不那么便携且使用起来更复杂。
回答by S.Lott
回答by eemz
"glob" is incredibly slow if there are many files.
如果有很多文件,“glob”会非常慢。
glob is slow is there are a lot of results because it returns one huge list of all the results in a "big bang" approach. If there are a lot of results this will use a lot of memory and take a long time.
glob 很慢,因为它有很多结果,因为它以“大爆炸”方法返回所有结果的一个巨大列表。如果有很多结果,这将使用大量内存并花费很长时间。
Use iglob instead, as suggested by a previous poster. iglob constructs and returns one iterator object that can be used to loop over the results, without ever having them all in memory at the same time. It's much more efficient if there are a lot of matches.
正如之前的海报所建议的那样,请改用 iglob。iglob 构造并返回一个迭代器对象,该对象可用于循环遍历结果,而不必同时将它们全部保存在内存中。如果有很多匹配项,效率会更高。
In general, whenever you write code like "for x in [glob.glob ...]" you should be using glob.iglob instead.
通常,每当您编写“for x in [glob.glob ...]”之类的代码时,您都应该使用 glob.iglob。
回答by ghostdog74
import os
import shutil
path=os.path.join("/home","mypath")
destination=os.path.join("/destination","dir")
for r,d,f in os.walk(path):
for files in f:
if files.endswith(".xml"):
try:
shutil.copy(os.path.join(r,files) , destination)
except IOError,e:
print e