How to take input file from terminal for python script?

Question:

I have a python script which uses a text file and manipulate the data from the file and output to another file. Basically I want it to work for any text file input. Right now I readline from the file and then print the output to screen. I want the output in a file.

So user can type the following and test for any file:

cat input_file.txt | python script.py > output_file.txt.

How can I implement this in my script? Thank You.

cat is command in linux. I don’t know how it works.

Asked By: Hell Man

||

Answers:

cat input_file.txt | python script.py > output_file.txt.

You can passing a big string that has all the data inside input_file.txt instead of an actual file so in order to implement your python script, just take that it as a string argument and split the strings by new line characters, for example you can use “n” as a delimiter to split that big string and to write to an outputfile, just do it in the normal way

i.e. open file, write to the file and close file

Answered By: JoeC

Sending output to a file is very similar to taking input from a file.

You open a file for writing the same way you do for reading, except with a 'w' mode instead of an 'r' mode.

You write to a file by calling write on it the same way you read by calling read or readline.

This is all explained in the Reading and Writing Files section of the tutorial.

So, if your existing code looks like this:

with open('input.txt', 'r') as f:
    while True:
        line = f.readline()
        if not line:
            break
        print(line)

You just need to do this:

with open('input.txt', 'r') as fin, open('output.txt', 'w') as fout:
    while True:
        line = fin.readline()
        if not line:
            break
        fout.write(line)

If you’re looking to allow the user to pass the filenames on the command line, use sys.argv to get the filenames, or use argparse for more complicated command-line argument parsing.

For example, you can change the first line to this:

import sys
with open(sys.argv[1], 'r') as fin, open(sys.argv[2], 'w') as fout:

Now, you can run the program like this:

python script.py input_file.txt outputfile.txt
Answered By: abarnert

The best way to do this is probably to call the input and output files as arguments for the python script:

import sys
inFile = sys.argv[1]
outFile = sys.argv[2]

Then you can read in all your data, do your manipulations, and write out the results:

with open(inFile,'r') as i:
    lines = i.readlines()

processedLines = manipulateData(lines)

with open(outFile,'w') as o:
    for line in processedLines:
        o.write(line)

You can call this program by running python script.py input_file.txt output_file.txt

If you absolutely must pipe the data to python (which is really not recommended), use sys.stdin.readlines()

Answered By: Kyle Neary

This method (your question) describes reading data from STDIN:

cat input_file.txt | python script.py

Solution: script.py:

import sys
for line in sys.stdin:
    print line

The method in above solutions describes taking argument parameters with your python call:

python script.py input_file.txt

Solution: script.py:

import sys
with open(sys.argv[1], 'r') as file:
    for line in file:
        print line

Hope this helps!

Answered By: Sticky
cat input_file.txt | python script.py > output_file.txt

Basically, python script needs to read the input file and write to the standard output.

import sys
with open('input_file.txt', 'r') as f:
    while True:
        line = f.readline()
        if not line:
            break
        sys.stdout.write(line)
Answered By: dkula
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.