0

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 ?

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

how about if i want to change the standard input to the given file in the command line. Something like python hello.py -i hello.in -o hello.out
Try this and let me know if it works python foo.py < in.txt > out.txt. I've edited my post to reflect the updates for this.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.