1
import fileinput
def main()
    try:
        lines = fileinput.input()
        res = process_lines(lines)

        ...more code
    except Exception:
        print('is your file path bad?')

if __name__ == '__main__':
    main()

When I run this code with a bad path, it doesn't throw an error, yet the docs say that an OS error will be thrown if there's an IO error. How do I test for bad paths then?

Błażej Michalik
  • 4,474
  • 40
  • 55
TheRealFakeNews
  • 7,512
  • 16
  • 73
  • 114
  • the error is thrown when you try to read/iterate the result of `input()`. Maybe the function `process_lines` is hiding the error? – Wups Oct 26 '20 at 17:03

1 Answers1

3

fileinput.input() returns an iterator, not an ad-hoc list:

In [1]: fileinput.input()
Out[1]: <fileinput.FileInput at 0x7fa9bea55a50>

Proper usage of this function is done via a for loop:

with fileinput.input() as files:
    for line in files:
        process_line(line)

or using a conversion to list:

lines = list(fileinput.input())

I.e. the files are opened only when you actually iterate over this object.

Although I wouldn't recommend the second way, as it is counter to the philosophy of how such scripts are supposed to work.

You are supposed to parse as little as you need to output data, and then output it as soon as possible. This avoids issues with large inputs, and if your script is used within a larger pipeline, speeds up the processing significantly.


With regards to checking whether the path is correct or not:

As soon as you'll iterate down to the file that doesn't exist, the iterator will throw an exception:

# script.py

import fileinput

with fileinput.input() as files:
    for line in files:
        print(repr(line))
$ echo abc > /tmp/this_exists
$ echo xyz > /tmp/this_also_exists
$ python script.py /tmp/this_exists /this/does/not /tmp/this_also_exists
'abc\n'
Traceback (most recent call last):
  File "/tmp/script.py", line 6, in <module>
    for line in files:
  File "/home/mrmino/.pyenv/versions/3.7.7/lib/python3.7/fileinput.py", line 252, in __next__
    line = self._readline()
  File "/home/mrmino/.pyenv/versions/3.7.7/lib/python3.7/fileinput.py", line 364, in _readline
    self._file = open(self._filename, self._mode)
FileNotFoundError: [Errno 2] No such file or directory: '/this/does/not'
Błażej Michalik
  • 4,474
  • 40
  • 55