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.
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 outFile = sys.argv
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