[Tutor] Zipfile and File manipulation questions.

Kent Johnson kent37 at tds.net
Tue Oct 17 12:04:41 CEST 2006


Chris Hengge wrote:
> I chose the way I used the names because to me...
> 
> outFile = open(aFile.lower(), 'w') # Open output buffer for writing.
> = open a file with lowercase name for writing.
> it is implied that aFile is from the zip, since it is created in the 
> loop to read the zip..
> 
> outFile.write(zFile.read(insideZip)) # Write the file.
> = write what is read from inside the zip file.

This sub-thread seems to have turned into "let's beat on Chris for the 
way he names things" which certainly isn't what I intended. Ultimately 
it is up to the program author to use the names that he thinks 
communicate most clearly.

But I think the actual naming was secondary to the my main point which 
is that the value for 'insideZip' is read from the zip, if you assign to 
that name and keep it in that name the code is easier to follow because 
the value that doesn't change stays in a name that doesn't change.

So using your names, it would read
             for insideZip in zFile.namelist():
                 for ext in ['.cap', '.hex', '.fru', '.cfg', '.sdr']:
                     if insideZip .lower().endswith(ext):
                         if "/" in insideZip :
                           aFile = aFile.rsplit('/', 1)[-1]
                         elif  "\\" in insideZip :
                           aFile = aFile.rsplit('\\', 1)[-1]
                         else:
                           aFile = insideZip

This way inzideZip is always the name from inside the zip, and aFile is 
always the name of the file to write.
> 
> I guess for declaration it isn't very clear, but thats what comments are 
> for?

The comments about comments have been on the mark, I think. A helpful 
guideline I use is, when I think I need a comment, look at the names I 
am using and see if I can create a function whose name conveys what I 
wanted to say in the comment.

Kent




More information about the Tutor mailing list