Reading a file using a UNC - help!

R

richard.kessler

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
(e-mail address removed)
 
R

Robert Kern

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"

\ is the escape character in strings, so if you need an actual backslash, you
need to double it:

fileList = "c:\\a_list_of_filenames.txt"

or use "raw" string literals, which don't do any escaping:

fileList = r"c:\a_list_of_filenames.txt"

Please see the tutorial:

http://docs.python.org/tut/node5.html#SECTION005120000000000000000

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
J

johnzenger

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.
 
R

richard.kessler

Thanks very much all for responding....I got it working. As you
indicated, I just had mixed up my escapes (//) with my \n. When I got
the correct amount of backslashes and removed the linefeeds it worked
great.

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top