3

I am trying to open a file and read from it and if the file is not there, I catch the exception and throw an error to stderr. The code that I have:

for x in l:
    try:        
        f = open(x,'r')
    except IOError:
        print >> sys.stderr, "No such file" , x

but nothing is being printed to stderr, does open create a new file if the file name doesn't exist or is the problem somewhere else?

6
  • Probably the file is there. And what does >> means after print Commented Mar 24, 2016 at 12:42
  • redirecting the output to standard error. I am pretty sure the file doesn't exist, because I am testing it with a file that is not there. yet nothing is printed out Commented Mar 24, 2016 at 12:44
  • Hm... it works fine for me. Ran from a script, no file in that directory and the error printed just fine. If you try to run open(x, 'r') without enclosing it in a try statement, do you get an error? Commented Mar 24, 2016 at 12:50
  • no i don't get an error if the file is there ,but when I should get an error, it doesn't print. the file name 'x' is from a list so I edited the code above, because I forgot to put in the loop. Not sure if that makes a difference though? Commented Mar 24, 2016 at 12:55
  • I still can't replicate your observation. Are you sure your script looks for files in the correct directory? Are you absolutely certain the file doesn't exist? Could you possibly have some error suppression enabled? Simply, if you run just open('asdfghj'), do you get an error? Assuming you don't have such a file with that name, of course. Commented Mar 24, 2016 at 13:05

3 Answers 3

4

Try this:

from __future__ import print_statement
import sys

if os.path.exists(x):
    with open(x, 'r') as f:
        # Do Stuff with file
else:
    print("No such file '{}'".format(x), file=sys.stderr)

The goal here is to be as clear as possible about what is happening. We first check if the file exists by calling os.path.exists(x). This returns True or False, allowing us to simply use it in an if statement.

From there you can open the file for reading, or handle exiting as you like. Using the Python3 style print function allows you to explicitly declare where your output goes, in this case to stderr.

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

1 Comment

Actually, putting the open() call inside of a try/except block is almost certainly better here -- the call to open can fail for reasons besides the file not existing (what happens in your code if the file exists, but the current process doesn't have read permission for it? BOOM -- an exception that will need to be caught.) This is a case where EAFP beats LBYL.
1

You have the os.path.exists function:

import os.path
os.path.exists(file_path)

returns bool

Comments

0

It works for me. Why can't you make use of os.path.exists()

for x in l:
    if not os.path.exists(x):
        print >> sys.stderr , "No such file", x

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.