How to take input file from terminal for python script?

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()

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)