Reading a file using a UNC - help!
johnzenger at gmail.com
johnzenger at gmail.com
Tue Sep 26 16:30:16 EDT 2006
You should use os.path.exists to test if a file exists. Your
exception-catching structure is not necessary.
Also, if the file you are reading from contains proper Windows
filenames, it is not necessary to replace \ with \\ and so forth. The
\ acts as an escape character only when it is in Python source code,
not when it is in a string read from the real world.
Also, because you wisely use "for x in inputfiles:" it is not necessary
to search for and replace newlines.
richard.kessler at matteicos.com wrote:
> I have the simplest need...to read a file full of file names(unc) and
> then check to see if each of these files exists. I tried with the
> following program, but always get file not found, even when it is
> there. If I type in the file name as a literal it works...
>
> Little program:
>
> #This module checks for file existence
> import string
> import sys
>
> def MainProcess():
>
> fileList = "c:\a_list_of_filenames.txt"
> inputFiles = open(fileList,"r")
>
> cnt = 0
> cntNotFound = 0
>
> for x in inputFiles:
> cnt = cnt + 1
> try:
> x = x.replace("\\","\\\\")
> x = x.replace("\n","")
> open(x,"rb")
> #open('\\\\myserver\\myshare\\myfile.dat',"r") #works when
> hard coded like this
> except IOError, (errno, strerror):
> print "I/O error(%s): %s" % (errno, strerror)
> cntNotFound = cntNotFound + 1
> print x + " Not Found"
> except ValueError, (errno,strerror):
> print "Some other error! " + str(errno) + " " + strerror
> else:
> print "Found " + x
>
> print str(cnt) + " total..."
> print str(cntNotFound) + " not found..."
>
> if __name__ == '__main__':
> MainProcess()
>
> Many thanks to anyone in advance to can tell me what I am doing wrong!
> Platform is XP with PythonWin.
>
> Richard Kessler
> richard.kessler at matteicos.com
More information about the Python-list
mailing list