Sending print output to a file in Python script [duplicate] - python

This question already has answers here:
Correct way to write line to file?
(17 answers)
Closed 7 years ago.
How would I go about sending the output of a print command to a new file? I have a python script where I need to redirect the output at the end of the print statement to a file but I can't seem to find a way to accomplish the redirect. Why doesn't "print (stuff to be redirected) > newfile.txt" work?
Any help is appreciated!

As mentioned in this post, you could set the standard output to a file object.
import sys
sys.stdout = open('file', 'w')
Then, all your print statements should go directly to that file.

Related

How to write help() documentation to a file Python? [duplicate]

This question already has answers here:
How do I export the output of Python's built-in help() function
(11 answers)
Closed 4 years ago.
So I was looking at the help() documentation of a module but soon realized it was very tedius to read the documentation in the small output box. So therefore I tried pasting the help() documentation to another file for more clearer reading.
myfile = open("file.txt","w")
myfile.write(str(help(random)))
myfile.close()
Instead of the documentation being written, it instead pasted in None.
Any ideas how to do this?
The answer is pydoc!. Run it from the console:
$ pydoc [modulename] > file.txt
and it will basically write the output of the help() command to file.txt
i'm not suggesting you should read the python documentation this way - but here is what you could do: you could redirect stdout and call help:
from contextlib import redirect_stdout
import random
with open('random_help.txt', 'w') as file:
with redirect_stdout(file):
help(random)
or, even simpler (as suggested by Jon Clements):
from pydoc import doc
import random
with open('random_help.txt', 'w') as file:
doc(random, output=file)

How to execute a python script and write output to a file? [duplicate]

This question already has answers here:
Output a python script to text file
(4 answers)
Closed 5 years ago.
i have the following script in python with a while loop
from time import sleep
while True:
print "hola"
print "mundo"
sleep(2)
and i want to write the output to a file with the following code:
import subprocess
with open("output.log", "w") as output:
subprocess.call(["python", "./main.py"], stdout=output);
the thing is that the while never ends, the file output.log never gets the output from the script, i wonder if there is a way to do it.
You can simply do it by the following command.
python filename.py > output.log
The above command works for both linux and windows.

How to run script over windows without any install [duplicate]

This question already has answers here:
How to compile python script to binary executable
(4 answers)
Closed 6 years ago.
if i create python code with .py and i want my friends get the code and it will work on theme computer (open files and print them).
how can i do that without install.
i mean i dont wont them yto install python.
i can do it as .exe or something?
thanks!
my code is stupid but required here so -
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
print "Read String is : ", str
# Close opend file
fo.close()
I would use Py2exe
but there are more solutions here are two links
link 1
link 2

How to Accept Command Line Arguments With Python Using < [duplicate]

This question already has answers here:
Python command line 'file input stream'
(3 answers)
Closed 8 years ago.
Is it possible to run a python script and feed in a file as an argument using <? For example, my script works as intended using the following command python scriptname.py input.txt and the following code stuffFile = open(sys.argv[1], 'r').
However, what I'm looking to do, if possible, is use this command line syntax: python scriptname.py < input.txt. Right now, running that command gives me only one argument, so I likely have to adjust my code in my script, but am not sure exactly how.
I have an automated system processing this command, so it needs to be exact. If that's possible with a Python script, I'd greatly appreciate some help!
< file is handled by the shell: the file doesn't get passed as an argument. Instead it becomes the standard input of your program, i.e., sys.stdin.
When you use the < operator in a shell you are actually opening the file and adding its contents to your scripts stdin
However there is is a python module that can do both. It's called fileinput.
https://docs.python.org/2/library/fileinput.html
It was shown in this post
How do you read from stdin in Python?
You can use the sys module's stdin attribute as a file like object.

How do I use the < > commands in my program? [duplicate]

This question already has answers here:
How do I read from stdin?
(25 answers)
Closed 18 days ago.
I have written a program that calculates the counts of each word in an input file. At the moment I am getting the filename using sys.argv[1], but I am actually supposed to be using
python word_counts.py < homer.txt > homer.test
I think homer.txt is the input file that is directed to my python script, while homer.test is the file that the output of my script is written to.
How do I make these work in my program?
The information in homer.txt is provided on standard-in. In python, that is a file handle called sys.stdin:
import sys
for line in sys.stdin: # reads from homer.txt
# process line
print(output) # writes to homer.test
homer.test is collecting data from standard-out. In python, the print statement writes to stdout by default. If you want to treat it explicitly as a file handle, you can use sys.stdout.
Use sys.stdin to read from homer.txt and sys.stdout (or print) to write to homer.test.

Categories

Resources