1

I'm new to python and just can't seem to get the code I'm writing to carry on processing if a file doesn't exist in the directory I point it to. Basically, I'm opening each text file to make a small amendment. I've tried putting the (what I thought) is the correct code here

for filename in find_files('a-zA-Z0-9', '*.txt'):
if os.path.isfile(filename):

with an else at the end of the code which prints a messge file not found. But I still get an IOError no such file or directory, which brings the whole program to a stop. What am I doing wrong?

Many thanks

user2377057
  • 183
  • 1
  • 1
  • 11
  • plz share the complete code. Without knowing what find_files() actually does it's hard to help. – pypat May 24 '13 at 09:39
  • 1
    how comes find_files returns a file that does not exist ? are you deleting it from elsewhere ? in this case, you have a synchronization issue. Your file could be deleted after the test, but before you open it (just like it can be deleted after the find_file, but before the test isfile) – njzk2 May 24 '13 at 09:41
  • what does find_files do, btw ? – njzk2 May 24 '13 at 09:41
  • @njzk2 < this, usually you should attempt to open the file and catch the exception in a `try` `except`, avoiding race conditions – jamylak May 24 '13 at 09:42
  • `isfile()` doesn't throw that exception, so it is either coming from your `find_files()` function or you are trying to work on a file even if `isfile()` returns `False`. Or you have a sync issue. Post more complete code for more help – Pep_8_Guardiola May 24 '13 at 09:43
  • anyway: fix your indentation as well, in your code: you should indent the if-statement. – Peter Varo May 24 '13 at 09:47
  • I think you can find an answer here: http://stackoverflow.com/questions/3964681/find-all-files-in-directory-with-extension-txt-with-python – Imanol Luengo May 24 '13 at 09:48

1 Answers1

0

i dont know your "find_files" function but os.path.isfile() just returning a False even if the given path is invalid

>>> os.path.isfile("NOTEXIST:/really/not/exists")
False

Error must be in your "find_files" function.

chrisf
  • 3
  • 1