What is equivalent to Perl (<>) in Python? fileinput is not working properly

Perl uses:

 while (<>) { # process files given as command line arguments } 

In Python, I found:

 import fileinput for line in fileinput.input(): process(line) 

But what happens when the file specified on the command line does NOT exist?

python test.py test1.txt test2.txt filenotexist1.txt filenotexist2.txt test3.txt was given as an argument.

I tried various ways to use try: except: nextfile , but I could not get it to work.

For the above command line, the script should run for test1-3.txt , but just skip to the next file when the file is NOT found.

Perl does it very well. I searched this on the net, but I could not find the answer to this question anywhere.

+6
source share
6 answers
 import sys import os for f in sys.argv[1:]: if os.path.exists(f): for line in open(f).readlines(): process(line) 
+5
source

Something like that;

 import sys for f in sys.argv[1:]: try: data = open(f).readlines() process(data) except IOError: continue 
+3
source

Including the @Brian response into the generator and IOError , rather than testing for an existence that is larger than Pythonic, and then printing a warning on stderr on failure:

 import sys def read_files(files = None): if not files: files = sys.argv[1:] for file in files: try: for line in open(file): yield line except IOError, e: print >>sys.stderr, 'Warning:', e for line in read_files(): print line, 

Output ( baz file does not exist):

 $ python read_lines.py foo bar baz line 1 of foo line 2 of foo line 1 of bar line 2 of bar Warning: [Errno 2] No such file or directory: 'baz' 

You might want to embellish the error message a bit, but it may not be worth the effort.

+3
source

You can solve your problem with the fileinput module as follows:

 import fileinput input = fileinput.input() while True: try: process(input.next()) except IOError: input.nextfile() except StopIteration: break 

Unfortunately, you cannot use for a loop because an IOException breaks it.

+2
source

Perhaps you can play with the openhook option to manage a non-existing file.

0
source

I tried to implement the @VGE sentence, but my attempt was not too elegant. I would appreciate any suggestions for improving this.

 import sys, fileinput, errno, os class nosuchfile: def readlines(foo, bar): return [] def close(arg): pass EXITCODE=0 def skip_on_error (filename, mode): """Function to pass in as fileinput.input(openhook=...) hook function. Instead of give up on the first error, skip the rest of the file and continue with the next file in the input list. In case of an error from open() an error message is printed to standard error and the global variable EXITCODE gets overwritten by a nonzero value. """ global EXITCODE try: return open(filename, mode) except IOError, e: sys.stderr.write ("%s: %s: %s\n" % (sys.argv[0], filename, os.strerror(e.errno))) EXITCODE = 1 return nosuchfile() def main (): do_stuff(fileinput.input(openhook=skip_on_error)) return EXITCODE 

Both classes of the dummy descriptor file nosuchfile and the global variable EXITCODE are pretty serious warts. I tried to figure out how to pass the reference to the local variable exitcode, but refused.

It also prevents the handling of errors that occur during reading, but most error cases seem to occur in open anyway.

0
source

All Articles