473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reading a file using a UNC - help!

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
ri*************@matteicos.com

Sep 26 '06 #1
3 2216
ri*************@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"
\ 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.htm...00000000000000

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

Sep 26 '06 #2
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.

ri*************@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
ri*************@matteicos.com
Sep 26 '06 #3
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.

Sep 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
8
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
2
by: Jean-Marie Vaneskahian | last post by:
Reading - Parsing Records From An LDAP LDIF File In .Net? I am in need of a .Net class that will allow for the parsing of a LDAP LDIF file. An LDIF file is the standard format for representing...
9
by: dba123 | last post by:
I need some help and direction on what classes and an example or two (article) on how to read an Excel Worksheet and insert one column into a database table column. I am using .NET 2.0 only. What...
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.