.app在使用python复制后,将无法在OS X上打开

2024-04-20 06:58:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python应用程序,它应该将OSX上的.app复制到一个新的地方,然后运行它。我在复制之前和之后对文件执行SHA1校验和,以确保复制没有错误,并且校验和已签出。有人能想到为什么会这样吗?我还确保文件权限在复制的.app上完全打开chmod 777,但没有修复它。你知道吗

这是python中copy函数的核心。这是从其他地方提取的一个稍微编辑过的:

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()

Tags: thepathinselfforifosdirectory
1条回答
网友
1楼 · 发布于 2024-04-20 06:58:39

您可以使用来自shutilscopytree调用来更新进度。举个例子:

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)

相关问题 更多 >