Reading the first line of a file (in a zipfile)
larry.bates at websafe.com
Wed Apr 11 21:56:10 CEST 2007
mike.aldrich at gmail.com wrote:
> 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,
> 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)
More information about the Python-list