2017-04-21 206 views
1

如何在Python中遞歸複製目錄(cp -r)?如何在python中執行linux中的「cp -r」

os.copytree結果在FileExistsError: [Errno 17] File exists:

而且distutils.dir_util.copy_tree提高AttributeError: module 'distutils' has no attribute 'dir_util'

如何在Python上cp -r執行在Linux相同呢?

+0

一種方法使用子做的是: '導入子流程' 'subprocess.call('cp -r source destination',shell = True)' –

+0

'system(「cp -r」)'may suitable you –

回答

1

使用distutils,您可能要導入這樣的,如果你注意到AttributeError

import distutils 
from distutils import dir_util 
distutils.dir_util.copy_tree("sourceDir", "dstDir") 

或者,你可以使用subprocess

import subprocess 
subprocess.call('cp -r sourceDir dstDir', shell=True)