我有这个python应用程序,应该将OS X上的.app复制到一个新的地方,然后运行它。我在复制之前和之后对文件执行SHA1校验和,以确保副本没有错误,并且校验和检出。任何人都可以想到为什么会这样吗?我也确保文件权限在复制的.app上完全打开chmod 777,但没有修复它。
这是我在python中复制函数的核心。这是从其他地方略微编辑的:
def copy_directory_recursive(self, srcPath, dstPath):
dirAndFileList = os.listdir(srcPath)
dirList = []
fileList = []
for i in dirAndFileList:
if os.path.isdir(os.path.join(srcPath, i)):
dirList.append(i)
elif os.path.isfile(os.path.join(srcPath, i)):
fileList.append(i)
dirList.sort()
fileList.sort()
for directory in dirList:
# if qItem.HasCancelled(): #If user has cancelled the copy, quit
# break
os.mkdir(os.path.join(dstPath, directory))
newSrc = os.path.join(srcPath, directory)
newDst = os.path.join(dstPath, directory)
# Call itself to do move into the newly created subfolder and
# repeat...
self.copy_directory_recursive(newSrc, newDst)
for file in fileList:
# if qItem.HasCancelled(): #If user has cancelled the copy, quit
# break
# Copy the file
shutil.copyfile(
os.path.join(srcPath, file), os.path.join(dstPath, file))
self.filesCopied += 1
# Feedback to the progress bar of the queue Item
self.update()
答案 0 :(得分:0)
您可以使用copytree
发出的shutils
来电并继续更新进度。一个例子:
def update(self):
proportionDone = self.filesCopied/float(self.totalFiles)
# Do whatever other work necessary here to show the progress bar
print "I'm %.2f percent done!" % (proportionDone * 100)
def progress(self, path, names):
self.filesCopied += len(names)
self.update()
return []
def copy_directory_recursive(self, srcPath, dstPath):
self.filesCopied = 0
self.totalFiles = 0
for root, dirs, files in os.walk(srcPath):
self.totalFiles += len(files) + len(dirs)
shutil.copytree(srcPath, dstPath, ignore=self.progress)