python save output to file

Do you want to know the details regarding “python save output to file”. If yes, you’re in the correct tutorial.

python save output to file

#first arg is the name of the file
#second arg notes that the file is open to write to it
outputFile = open("fileName", "w")
#next line writes to the file
outputFile.write(str)
#remember to close opened files
outputFile.close()
print("Hello stackoverflow!", file=open("output.txt", "a"))
print("I have a question.", file=open("output.txt", "a"))
with open("output.txt", "a") as f:
    print("Hello StackOverflow!", file=f)
    print("I have a question.", file=f)
$ python my_program.py > output.txt

Conclusion

I hope this article helps you to know about “python save output to file”. If you have any doubts regarding this tutorial please let us know via the comment section. Share this article with your friends and family via social networks.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coder Diksha. Here, I post about programming to help developers.

Share on:

Leave a Comment