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.
Related
This question already has answers here:
How to read/process command line arguments?
(22 answers)
Closed last year.
Long story short I want to do a project but don't even know where to start or how to describe it for a google search.
When I run my python file in the Command Prompt I want to be able put a text file next to it that my program will read.
Like: D:> python3 name_of_file.py name_of_text_file.txt
Now when I execute the above I want my python file to somehow see the text file to do something with it (like outputting the contents) but I don't know how to do that. Any suggestions?
Now since I don't know where to start I do not have anything to show on what I have already tried.
Incase it helps: I am using Atom. & The python file and the text file are saved in the same folder.
Within a python script you can use the list sys.argv. The first list element (sys.argv[0]) is the name of the python script file (in your case "name_of_file.py"). The rest of the list elements are the provided command line arguments. So sys.argv[1] will contain the first command line argument.
import sys
# Retrieve first command line argument
file_name = sys.argv[1]
# Open text file
with open(file_name) as file:
# Read out contents of text file
file_text = file.read()
print(file_text)
I have an algorithm that is written in C++ that outputs a cout debug statement to the terminal window and I would like to figure out how to read that printout with python without it being piped/written to a file or to return a value.
Python organizes how each of the individual C++ algorithms are called while the data is kept on the heap and not onto disk. Below is an example of the a situation that is of similar output,
+-------------- terminal window-----------------+
(c++)runNewAlgo: Debug printouts on
(c++)runNewAlgo: closing pipes and exiting
(c++)runNewAlgo: There are 5 objects of interest found
( PYTHON LINE READS THE PRINT OUT STATEMENT)
(python)main.py: Starting the next processing node, calling algorithm
(c++)newProcessNode: Node does work
+---------------------------------------------------+
Say the line of interest is "there are 5 objects of interest" and the code will be inserted before the python call. I've tried to use sys.stdout and subprocess.Popen() but I'm struggling here.
Your easiest path would probably be to invoke your C++ program from inside your Python script.
More details here: How to call an external program in python and retrieve the output and return code?
You can use stdout from the returned process and read it line-by-line. The key is to pass stdout=subprocess.PIPE so that the output is sent to a pipe instead of being printed to your terminal (via sys.stdout).
Since you're printing human-readable text from your C++ program, you can also pass encoding='utf-8' as well to automatically decode each line using utf-8 encoding; otherwise, raw bytes will be returned.
import subprocess
proc = subprocess.Popen(['/path/to/your/c++/program'],
stdout=subprocess.PIPE, encoding='utf-8')
for line in proc.stdout:
do_something_with(line)
print(line, end='') # if you also want to see each line printed
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.
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.
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.