Reading the first line of a file (in a zipfile)

M

mike.aldrich

Hi folks,
I am trying to read the first occurence of non-whitespace in a file,
within a zipfile. Here is my code:

zipnames = glob.glob("<search_dir>*")
for zipname in zipnames:
z = zipfile.ZipFile(zipname, "r")
for filename in z.namelist():
count = len(z.read(filename).split('\n'))
if fnmatch.fnmatch(filename, "*AUDIT*"):
test = filename.split(' ')
print 'File:', test[0],
bytes = z.read(filename)
print 'has', len(bytes), 'bytes'
print 'and', count, 'lines'

The first line in the file I am examining will be a number followed by
more whitespace. Looks like I cannot split by whitespace?
 
L

Larry Bates

Hi folks,
I am trying to read the first occurence of non-whitespace in a file,
within a zipfile. Here is my code:

zipnames = glob.glob("<search_dir>*")
for zipname in zipnames:
z = zipfile.ZipFile(zipname, "r")
for filename in z.namelist():
count = len(z.read(filename).split('\n'))
if fnmatch.fnmatch(filename, "*AUDIT*"):
test = filename.split(' ')
print 'File:', test[0],
bytes = z.read(filename)
print 'has', len(bytes), 'bytes'
print 'and', count, 'lines'

The first line in the file I am examining will be a number followed by
more whitespace. Looks like I cannot split by whitespace?
You have told split to split on single blank space not whitespace.
To split on whitespace use .split() (e.g. no arguments)

-Larry
 
G

Gabriel Genellina

Hi folks,
I am trying to read the first occurence of non-whitespace in a file,
within a zipfile. Here is my code:

zipnames = glob.glob("<search_dir>*")
for zipname in zipnames:
z = zipfile.ZipFile(zipname, "r")
for filename in z.namelist():
count = len(z.read(filename).split('\n'))
if fnmatch.fnmatch(filename, "*AUDIT*"):
test = filename.split(' ')
print 'File:', test[0],
bytes = z.read(filename)
print 'has', len(bytes), 'bytes'
print 'and', count, 'lines'

The first line in the file I am examining will be a number followed by
more whitespace. Looks like I cannot split by whitespace?

Your code does nothing with the first line on the file; you only split the
*filename* on whitespace. And you extract the file twice.
You don't even try to find "the first occurence of non-whitespace". Surely
an example of file contents and what output you really expect from it
would be adequate.
 
M

mike.aldrich

En Wed, 11 Apr 2007 16:13:42 -0300, <[email protected]> escribió:




Hi folks,
I am trying to read the first occurence of non-whitespace in a file,
within a zipfile. Here is my code:
zipnames = glob.glob("<search_dir>*")
for zipname in zipnames:
z = zipfile.ZipFile(zipname, "r")
for filename in z.namelist():
count = len(z.read(filename).split('\n'))
if fnmatch.fnmatch(filename, "*AUDIT*"):
test = filename.split(' ')
print 'File:', test[0],
bytes = z.read(filename)
print 'has', len(bytes), 'bytes'
print 'and', count, 'lines'
The first line in the file I am examining will be a number followed by
more whitespace. Looks like I cannot split by whitespace?

Your code does nothing with the first line on the file; you only split the
*filename* on whitespace. And you extract the file twice.
You don't even try to find "the first occurence of non-whitespace". Surely
an example of file contents and what output you really expect from it
would be adequate.

The file contents have leading whitespace, then a number:
123456 \n
I expect to return '123456'
 
M

mike.aldrich

And nothing following the number?

py> line = " 123456 \n"
py> print line.strip()
123456

That works fine if I am using the interpreter, but I get 'cannot open
file' when i try to read from an archive..
Does that make sense? Sorry, this is my 2nd python script.
 
G

Gabriel Genellina

That works fine if I am using the interpreter, but I get 'cannot open
file' when i try to read from an archive..
Does that make sense? Sorry, this is my 2nd python script.

Try a small, failing example and post the code and the full error
traceback - else it's hard to tell what's happening.
 
7

7stud

Hi folks,

The first line in the file I am examining will be a number followed by
more whitespace. Looks like I cannot split by whitespace?
but I get 'cannot open
file' when i try to read from an archive..

....and that led you to conclude that you cannot split by whitespace?
 

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

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top