0

I am using sys.stdin in my code, and I want to know how to test my code on multiple text files. My code(test.py) is:

for line in sys.stdin:
   line = line.strip()
   words = line.split()

I am trying to test it on 2 text files, so I type in terminal:

echo "test1.txt" "test2.txt" | test.py

but it not works, so I just want to know how can I test the code on 2 text files?

Alex Z
  • 347
  • 1
  • 2
  • 13

1 Answers1

1
echo "test1.txt" "test2.txt" | test.py

Won't actually run test.py, you need to use this command instead:

echo "test1.txt" "test2.txt" | python test.py

However, another method for getting arguments into python would be:

import sys
for arg in sys.argv:
    print line

Which when run like so:

python test.py "test1" "test2"

Produces the following output:

test.py
test1
test2

The first argument of argv is the name of the program. This can be skipped with:

import sys
for arg in sys.argv[1:]:
    print line

A further problem you appear to be having is you're assuming that python is opening the text files you're handing it in the loop - this isn't true. If you print in the loop you'll see it's only printing the strings you gave it initially.

If you actually want to open and parse the files, do something like this in the loop:

import sys

args = sys.stdin.readlines()[0].replace("\"","").split()
for arg in args:
    arg = arg.strip()
    with open(arg, "r") as f:
        for line in f:
            line = line.strip()
            words = line.split()

The reason we have that weird first line is that stdin is a stream, so we have to read it in via readlines().

The result is a list with a single element (because we only gave it one line), hence teh [0]

Then we need to remove the internal quotes, because the quotes aren't really required when piping, this would also work:

echo test1.txt test2.txt | python test.py

Finally, we have to split the string into the actual filenames.

Darkstarone
  • 4,590
  • 8
  • 37
  • 74
  • Thank you! I change the code to the last loop you provide and run it by type: python test.py "test1.txt" but it end up in error like No such file or directory: 'test1.txt\n' with a strange '\n'. I do put the test1.txt and py file in the same folder. So may I ask how to fix that? – Alex Z Apr 22 '17 at 02:31
  • Note: `| test.py` could work providing you have the appropriate shebang, e.g. `#!/usr/bin/env python` and appropriate permissions `chmod +x test.py` – AChampion Apr 22 '17 at 02:46
  • Thank you very much, now it works for single input file. However, when I trying to apply it to multiple files: echo "test.txt" "eco.txt" | python test.py, It return errors like:No such file or directory: 'test.txt eco.txt'. may I ask how to avoid this issue? – Alex Z Apr 22 '17 at 02:47
  • Updated my answer, it's a bit more complicated than I initially thought - which is why I showed you `sys.argv` at the start - it's the conventional way to add arguments to a python file. – Darkstarone Apr 22 '17 at 03:02