Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
运行bash命令在Python3中复制粘贴文件是可靠的选择吗?_Python_Python 3.x - Fatal编程技术网

运行bash命令在Python3中复制粘贴文件是可靠的选择吗?

运行bash命令在Python3中复制粘贴文件是可靠的选择吗?,python,python-3.x,Python,Python 3.x,使用以下代码在Python3中复制文件需要花费大量时间: shutil.copy(self.file,self.working\u dir) 然而,Linux的cp命令非常快。如果我尝试从Python3执行bash命令来复制大小大于100GB的文件,这对生产服务器来说是可靠的选择吗 我看到了答案,但它的建议并不快。如果您在Windows上运行,Python的复制缓冲区大小可能太小: 您需要实现类似的功能(警告:未测试): 使用shutil.copy()可能会遇到的一个问题是,在POSIX平台上,

使用以下代码在Python3中复制文件需要花费大量时间:

shutil.copy(self.file,self.working\u dir)

然而,Linux的
cp
命令非常快。如果我尝试从Python3执行bash命令来复制大小大于100GB的文件,这对生产服务器来说是可靠的选择吗


我看到了答案,但它的建议并不快。

如果您在Windows上运行,Python的复制缓冲区大小可能太小:

您需要实现类似的功能(警告:未测试):


使用
shutil.copy()
可能会遇到的一个问题是,在POSIX平台上,文件所有者和组,以及传输期间的ACL
shutil.copyfile()
应该可以满足您的需要。您是否已经运行了任何基准,并看到实际性能下降?另一件需要考虑的是,对于100GB的文件,使用主线程可能会阻塞很多,这可能不是您想要的,因此一个新的线程或子进程将非常需要后台大型复制任务。
def copyfile_largebuffer(src, dst, length=16*1024*1024):
    with open(newfile, 'wb') as outfile, open(oldfile, 'rb') as infile:
        copyfileobj_largebuffer(infile, outfile, length=length)

def copyfileobj_largebuffer(fsrc, fdst, length=16*1024*1024):
    while 1:
        buf = fsrc.read(length)
        if not buf:
            break
        fdst.write(buf)