Reputation: 431
I need to enter the contents of a text (.txt) file as input for a Python (.py) file. Assuming the name of the text file is TextFile and the name of the Python file PythonFile, then the code should be as follows:
python PythonFile.py < TextFile.txt
Yet, when I try to do this in IDLE and type in
import PythonFile < TextFile,
IDLE gives me an invalid syntax message, pointing to the <
sign. I tried all sorts of variations on this theme (i.e.,using or not using the file name extensions), but still got the same invalid-syntax message. How is the syntax different for input redirection in IDLE?
Upvotes: 2
Views: 984
Reputation: 19154
If it works in the command line, then why do you want to do this in IDLE? There are ways to achieve a similar result using, for example, subprocess
, but a better way would be to refactor PythonFile.py
so that you can call a function from it, e.g.:
>>> import PythonFile
>>> PythonFile.run_with_input('TextFile.txt')
If you post the contents of PythonFile.py
, we might be able to help you do this.
Upvotes: 1