简体   繁体   中英

run python from command line windows not working

I am trying to run the python script in windows command line. Script is executing successfully. But when i try to redirect the output to file, it is still printing in the console.

python hello.py -o hello.out

am i doing anything wrong ?

I think you should be doing this:

python hello.py > hello.out

** EDIT ** An additional question was asked about stdin.

From the command-line you can do something like this:

python foo.py < in.txt > out.txt

Inside foo.py you need to make sure you are grabbing stdin from somewhere, so do something like this:

import fileinput
for line in fileinput.input():
    print(line)
    # do stuff with each line of input

Unless your script supports -o itself, I do not expect that flag to do anything. python does not have such an option, and if it did, you would have to put it before the name of your script so that it could be identified as an option to the interpreter instead of your script.

Here is a good article on how to redirect output in Windows: https://support.microsoft.com/en-us/kb/110930 . Surprisingly, it is done the same was as in Linux, using the > character. Your example should be written as either

python hello.py > hello.out

or as

python hello.py > hello.out 2>&1

if you want to include standard error as well as standard output in your file.

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