0

If I want to print the results of a python script or an other program to a file, I simple do something like this on the console python myscript.py>output.txt, but now myscript has an input inside and how can I get this to the console? For example I have script to check if a given year is a leap year or not:

def isLeapYear(year):
    return (year%4==0 and (year%400==0 or year%100!=0))

if __name__ == "__main__":
    y=input("Test if your year is a leap year: ")
    try:
        out=str(y)+" is"
        if not isLeapYear(int(y)):
              out+=" not";
        print(out+" a leap year")
    except ValueError:
        print("invaild input")

When I now write the command above, the result in my output-file is something like this (while I can just enter the year in the console):

Test if your year is a leap year: 2000 is a leap year

but I just want

2000 is a leap year

in there and have

Test if your year is a leap year:

shown on the console. Is there a clever way to do this? Can I somehow force print something to the console? Is there a general way to this? I mean I could have the same problem with a C or Java-Program.

user3284214
  • 21
  • 1
  • 4
  • 1
    It is probably best to implement writing to file in the script itself. You could pass the output filename as argument from command line. – zvone Dec 10 '15 at 11:46
  • I figured this much. Can I least create my script in such a way that if someone tries to produce a file-output in such a way, that an error message is shown, while the script is never run? – user3284214 Dec 10 '15 at 12:03
  • You don't need to convert your input to str since every input is a string unless you modify it, for the output not to include "Test if your year is a leap year:" simply do not add the answer to "out" variable of yours. In the statement out+="is" you are adding "is" to your input – Amir Afianian Dec 10 '15 at 12:44
  • @user3284214 You cannot know what happens to your output once it is out. If someone decides to redirect it to a file, it will go to a file. They might also pipe it to another application, or pack it in a box and send to outer space. You should not trouble yourself with that. – zvone Dec 10 '15 at 13:22

2 Answers2

0

According to the comments above:

*) You should handle the file writing in your python script, as we're in Python it's just a few lines of code.

*) About your question what would happen if someone still calls the script in the way that the output should be redirected, you can check in python if stdin and stdout are the same, if not the output got redirected according to Determining if stdout for a Python process is redirected

To safe you one more click here's the example code, test.py is a two-liner that does exactly this check:

    $ cat test.py
    import os
    print os.fstat(0) == os.fstat(1)
    $ python test.py
    True
    $ python test.py > f
    $ cat f
    False
    $

Hope that helps.

Cheers, Peter

Community
  • 1
  • 1
p9teufel
  • 99
  • 1
  • 5
0

If you want to keep the terminal printing I'd just edit the script to write the output.

outfile = open("leap-output.txt", "a+")

Then add the writing to the script.

outfile.write("%s a leap year") %(out)
outfile.write("\n")
outfile.close()

Redirecting the output with > is a bash function and you don't have a control over that.

As said by zvone you gain some control if you force users to give arguments the script ie.

userinput = sys.argv[1]

Then you'd start your script by typing python myscript.py example-output-file.txt

Python will crash here if user hasn't given the argument for the outfile.

jester112358
  • 465
  • 3
  • 17