为什么从我的python脚本写入文件被覆盖 [英] Why is writing to a file from my python script getting overwritten

查看:207
本文介绍了为什么从我的python脚本写入文件被覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个简单的操作系统问题,而不是编程问题,因此我们可能需要将其迁移到另一个社区.

我犯了一个错误,遇到了一些奇怪的行为,我想了解正在发生的事情.

在我的python脚本中,我调用了某人编写的Java程序.他们使用Logger记录一些消息,而我正在将Java程序输出写入outfile.我也想将脚本的输出重定向到另一个文件b/c,我不想从Java程序中搜索大量的msgs.

out = sys.argv[2]
...
...
with open(out, 'w') as outfile:
   # call some java programs via Popen but log 
   # some msgs after all the java programs have run
   cmd = ["java", "-cp", "blah.jar", "com.main.blah", "param1", "param2"]
   proc = subprocess.Popen(cmd, stdout=outfile)
   proc.wait()
....
....
print "got here to test where this msg gets written"

运行脚本时,我偶然将stdout和stderr重定向到了相同的文件名.

python script.py input output > output 2>&1

由于这次事故,我遇到了这种奇怪的行为.似乎重定向的stdout和stderr被写入文件输出"的开头而不是结尾,从而覆盖了Java程序中所有现有的msg.

我很好奇为什么会这样.我猜想python脚本的文件指针一直向前移动,因为java程序一直在写日志消息,但是OS重定向的文件指针没有移动b/c,它不知道脚本在写什么,因此,当轮到重定向时,它在认为文件结尾的位置写了东西.这样对吗?希望有人能启发我这种情况.谢谢.

P.S.我写了正确"的命令,java程序的输出确实转到了一个文件,重定向确实转到了另一个文件

更新: 感谢所有的意见和评论/答案.我认为我的问题措词不正确,因此我可以删除此问题.我认为这不是重复的问题.这不是将stdout和stderr都附加或写入同一文件的问题.我知道该怎么做.这是一个关于两件事试图写入同一文件但输出混合在一起的问题.我对正在发生的事情有一个猜测,并将其放在我的原始帖子中,但似乎我写的内容不清楚.

解决方案

在bash中:

>将覆盖文件

>>将追加到文件

This might be a simple OS question rather than programming question so we might need to migrate this to another community.

I made a mistake and came across some weird behavior that I'd like to understand what's happening.

In my python script, I call a java program someone wrote. They use Logger to log some msgs and I'm writing the java program output to outfile. I also want to redirect the output of my script to another file b/c I don't want to search thru the sea of msgs from the java program.

out = sys.argv[2]
...
...
with open(out, 'w') as outfile:
   # call some java programs via Popen but log 
   # some msgs after all the java programs have run
   cmd = ["java", "-cp", "blah.jar", "com.main.blah", "param1", "param2"]
   proc = subprocess.Popen(cmd, stdout=outfile)
   proc.wait()
....
....
print "got here to test where this msg gets written"

When running my script, I redirected stdout and stderr to the same file name by accident.

python script.py input output > output 2>&1

Due to this accident, I came across this weird behavior. It seems like the redirected stdout and stderr gets written at the beginning of the file "output" rather than the end and thus overwrites any existing msgs from the java program

I'm just curious why this is happening. I'm guessing that the file pointer for the python script kept moving forward because the java program kept writing log msgs but the file pointer for the OS redirect didn't move b/c it didn't know anything was being written by the script, and thus, when the redirect's turn came, it wrote where it thinks is the end of the file. Is this correct? Hopefully someone can enlighten me on this situation. Thanks.

P.S. I wrote the command "correctly" and the java program output did go to one file and the redirect did go to another file

UPDATE: Thanks to all the views and comments/answers. I think I didn't word my question correctly so I might delete this question. I don't think this is a duplicate question. This is not a question on appending or writing both stdout and stderr to the same file. I know how to do that. This is a question on having two things trying to write to the same file but the output gets all mixed up. I had a guess as to what was happening and put that in my original post but it seems like what I wrote isn't clear.

解决方案

In bash:

> will overrite a file

>> will append to a file

这篇关于为什么从我的python脚本写入文件被覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆