python代码是否可能在子进程完成之前执行?

2024-09-28 22:26:21 发布

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

我写的一个脚本,将几个PDF压缩成一个在我的电脑上运行良好,但似乎只在我的一个同事的电脑上产生损坏的PDF

我很难调试这个问题,因为它需要访问他的个人电脑。但是,我认为问题是子进程和临时文件清理函数之间的时间问题。该脚本将网络驱动器位置上的几个单独的PDF压缩为同一位置上的单个临时PDF。然后,它利用子进程将临时PDF复制到用户选择的位置(在本例中是指他们的桌面)。但是,下一行代码是我的临时文件清理例程,在该例程中,除了用户桌面上的副本之外,所有PDF都被删除。有没有可能在复制过程中,清理例程执行并删除它?我有理由相信,临时PDF正在适当地创建,这似乎只发生在他的机器上

以下是有关脚本的部分:

    #Condense Seperate PDFs to Single PDF
    writer = PdfWriter()
    for pdf in pdfs:
        if os.path.exists(pdf):
            writer.addpages(PdfReader(pdf).pages)
            pages = pages + 1
    writer.write("tempFinal.pdf")

    #Copy Final PDF to the Save Path (Users Desktop in this example)
    subprocess.call('copy "tempFinal.pdf" "'+ savePath +'" /y', shell=True, stdout=DEVNULL, stderr=subprocess.DEVNULL)

    #Clean-up all temp files.
    deleteTemp()

以下是清理例程:

def cleanup(filename):
    try:
        os.remove(filename)
    except OSError:
        pass

def deleteTemp():
    #pdfs is a list of the seperate PDF files
    for pdf in pdfs:
        cleanup(pdf)
    cleanup("tempFinal.pdf")
    cleanup("temp.dxf")
    cleanup("plot.log")
    cleanup("plot.scr")

Tags: to用户in脚本forpdf进程pages