简体   繁体   English

Python,子流程和文本文件创建

[英]Python, subprocesses and text file creation

Apologies if this kind of thing has been answered elsewhere. 抱歉,如果在其他地方也回答过这种问题。 I am using Python to run a Windows executable file using subprocess.Popen() . 我正在使用Python使用subprocess.Popen()运行Windows可执行文件。 The executable file produces a .txt file and some other output files as part of its operation. 可执行文件会生成一个.txt文件和一些其他输出文件,作为其操作的一部分。 I then need to run another executable file using subprocess.Popen() that uses the output from the original .exe file. 然后,我需要使用subprocess.Popen()运行另一个可执行文件,该文件使用原始.exe文件的输出。

The problem is, it is the .exe file and not Python that is controlling the creation of the output files, and so I have no control over knowing how long it takes the first text file to write to disk before I can use it as an input to the second .exe file. 问题是,.exe文件而不是Python来控制输出文件的创建,因此我无法控制在将第一个文本文件用作磁盘之前需要花费多长时间来写入第一个文本文件。输入到第二个.exe文件。

Obviously I cannot run the second executable file before the first text file finishes writing to disk. 显然,在第一个文本文件完成写入磁盘之前,我无法运行第二个可执行文件。

subprocess.wait() does not appear to be helpful because the first executable terminates before the text file has finished writing to disk. subprocess.wait()似乎没有帮助,因为第一个可执行文件在文本文件完成写入磁盘之前终止。 I also don't want to use some kind of function that waits an arbitrary period of time (say a few seconds) then proceeds with the execution of the second .exe file. 我也不想使用某种函数,它会等待任意时间段(例如几秒钟),然后继续执行第二个.exe文件。 This would be inefficient in that it may wait longer than necessary, and thus waste time. 这将是低效的,因为它可能等待的时间超过了必要的时间,从而浪费了时间。 On the other hand it may not wait long enough if the output text file is very large. 另一方面,如果输出文本文件很大,它可能不会等待足够长的时间。

So I guess I need some kind of listener that waits for the text file to finish being written before it moves on to execute the second subprocess.Popen() call. 所以我想我需要某种侦听器,等待文本文件完成写入,然后继续执行第二个subprocess.Popen()调用。 Is this possible? 这可能吗?

Any help would be appreciated. 任何帮助,将不胜感激。


UPDATE (see Neil's suggestions, below) 更新 (请参阅下面的尼尔建议)

The problem with os.path.getmtime() is that the modification time is updated more than once during the write, so very large text files (say ~500 Mb) require a relatively large wait time in between os.path.getmtime() calls. os.path.getmtime()的问题在于,修改时间在写入过程中多次更新,因此,非常大的文本文件(例如〜500 Mb)在os.path.getmtime()之间需要相对较长的等待时间。电话。 I use time.sleep() to do this. 我使用time.sleep()来做到这一点。 I guess this solution is workable but is not the most efficient use of time. 我想这种解决方案是可行的,但不是最有效的时间利用方式。

On the other hand, I am having bigger problems with trying to open the file for write access. 另一方面,尝试打开文件进行写访问时遇到了更大的问题。 I use the following loop: 我使用以下循环:

while True:
    try:
        f = open(file, 'w')
    except:
        # For lack of something else to put in here
        # (I don't want to print anything)
        os.path.getmtime(file) 
    else:
        break

This approach seems to work in that Python essentially pauses while the Windows executable is writing the file, but afterwards I go to use the text file in the next part of the code and find that the contents that were just written have been wiped. 这种方法似乎有效,因为Windows可执行文件正在编写文件时,Python基本上会暂停,但是此后,我去使用代码的下一部分中的文本文件,发现刚才编写的内容已被擦除。

I know they were written because I can see the file size increasing in Windows Explorer while the executable is doing its stuff, so I can only assume that the final call to open(file, 'w') (once the executable has done its job) causes the file to be wiped, somehow. 我知道它们是写的,因为在可执行文件执行任务时我可以在Windows资源管理器中看到文件大小的增加,因此,我只能假定对open(file,'w')的最终调用(一旦可执行文件完成了工作) )以某种方式导致文件被擦除。

Obviously I am doing something wrong. 显然我做错了。 Any ideas? 有任何想法吗?

There's probably many ways to do what you want. 可能有很多方法可以做您想要的。 One that springs to mind is that you could poll the modification time with os.path.getmtime() , and see when it changes. 我想到的是您可以使用os.path.getmtime()轮询修改时间,并查看修改时间。 If the modification date is after you called the executable, but still a couple seconds ago, you could assume it's done. 如果修改日期是在调用可执行文件之后,但还是在几秒钟前,则可以认为修改已完成。

Alternatively, you could try opening the file for write access (just without actually writing anything). 或者,您可以尝试打开文件进行写访问(只是不实际写任何东西)。 If that fails, it means someone else is writing it. 如果失败,则意味着其他人正在编写它。

This all sounds so fragile, but I assume your hands are somewhat tied, too. 所有这些听起来都很脆弱,但我想您的手也有些被绑住。

One suggestion that comes to mind is if the text file that is written might have a recognizable end-of-file marker to it. 我想到的一个建议是,写入的文本文件是否可能带有可识别的文件结尾标记。 I created a text file that looks like this: 我创建了一个文本文件,如下所示:

BEGIN
DATA
DATA
DATA
END

Given this file, I could then tell if "END" had been written to the end of the file by using os.seek like this: 给定此文件,然后可以使用os.seek判断是否已将“ END”写入文件os.seek

>>> import os
>>> fp = open('test.txt', 'r')
>>> fp.seek(-4, os.SEEK_END)
>>> fp.read()
'END\n'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM