English 中文(简体)
python 子进程正在覆盖用于 stdout 的文件 - 我需要它来附加到文件( 窗口) 。
原标题:python subprocess is overwriting file used for stdout - I need it to append to the file (windows)

我要将 STDOUT 附加到一个已有的文件。下面的代码会覆盖文件 --

log_file = open(log_file_path,  r+ )
cmd = r echo "some info for the log file" 
subprocess.call(cmd, shell=True, stdout=log_file, stderr=STDOUT)
log_file.close()

我在 sub process. call () sub process.Popen () 中寻找 等同的 .call () sub process.Popen () . 它让我抓狂了。

更新:

至今为止的答案之后,我更新了我的代码

import subprocess

log_file = open( test_log_file.log ,  a+ )
cmd = r echo "some info for the log file
" 
subprocess.call(cmd, shell=True, stdout=log_file, stderr=subprocess.STDOUT)
log_file.close()

我运行这个代码 从窗口的命令行-

C:usersaidan>test_subprocess.py

这将将文字添加到日志文件 。 当我再次运行脚本时, 没有添加任何新内容 。 它似乎仍然在覆盖文件 。

最佳回答

使用附件模式替代 :

log_file = open(log_file_path,  a+ )

如果您仍然看到先前的内容被覆盖, 也许 Windows 需要您明确寻找文件的结尾; 以 r+ 或 w 打开, 并寻找文件的结尾 :

import os

log_file = open(log_file_path,  r+ )
log_file.seek(0, os.SEEK_END)
问题回答

修改您如何打开日志_file_path。 您正在打开文件读写 r+ 。 使用附加模式而不是 r+ :

log_file = open(log_file_path,  a+ )




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...