0

I am writing a python program through a unix shell. While I have written the program, I had to hard code my data in. However my goal is to be able to be able to stdin the data through the shell like this

python3 sample.py data.txt

Where sample.py is the program and data.txt is the data. Data.txt contains two column of data seperated by a tab. Then I have print to make sure it is working. The way I wrote the code to read the data in is as follows

for line in sys.stdin:
    words = re.split(r'\t',line)
    print(words)

Splitting the content of the lines the tab, and printing to make sure it is working. When running line "python3 sample.py data.txt", it does nothing, however the program will not exit like there is an infinite loop or something. How can I get this to print?

1 Answer 1

2

With

python3 sample.py data.txt

you are passing data.txt as an argument. To pass its content as input (to stdin), do:

python3 sample.py < data.txt

The redirection operator < is a shell construct and refers to the stdin.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.