简体   繁体   中英

Python3: os.system not redirecting stdout

As mentioned in the title. I have this in my code:

os.system("./vpr/vpr " + config + " " + file_name + " --seed " + str(seed) + " &> " + str(bench_name) + "-" + str(seed) + ".stdout")

Which has a lot of variables, but it simply evaluates to this (I know for sure because I have a print statement right before the os.system line):

./vpr/vpr vpr/k6_N10_40nm.xml vpr/blif/clma.blif --seed 0 &> clma-0.stdout

The command actually runs fine, but the redirection does not! The file clma-0.stdout gets created but remains empty, and I still get the entire stdout on my terminal.

What is the solution for that? What am I doing wrong? I'm using python-3.7 on Ubuntu 19.10

Thanks.

I'm not sure why exactly, but it seems like os.system is using Dash (Ubuntu's default scripting shell), not Bash, so &> is not supported. What happens instead is the command is backgrounded, and the file is truncated. That is, command &> filename is equivalent to command &; > filename command &; > filename .

To fix it you could simply use the equivalent redirection, > filename 2>&1 .

I think that's because you are trying to do it using system command, not Bash which supports these I/O redirection flags.

Try this one with shell=True https://docs.python.org/2/library/subprocess.html#subprocess.call

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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