[Tutor] Zipfile and File manipulation questions.

Chris Hengge pyro9219 at gmail.com
Tue Oct 17 03:54:17 CEST 2006


I didn't come up with those variable names willy nilly... I chose them
because they reflect their functionality.. Which to me, is much more
important then anything else... if I go back and look at code months later..
I have an easier time remember names based on functionality, and less of an
easy time naming "because that word best describes it"

real world example...
I drive to work, I own a car... While car is the actual object description,
when I go to tell someone what I'm attempting to do with the car, 'drive' is
much more clear then car.

On 10/16/06, Luke Paireepinart <rabidpoobear at gmail.com> wrote:
>
> 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.
> >
> > I guess for declaration it isn't very clear, but thats what comments
> > are for?
> No!!!!
> Consider the following 2 examples:
>
> #temp.py
> def aa(a):
>     for b in range(2,a):
>         if a % b == 0:
>             return False
>     return True
>
> a = int(raw_input('Please enter a prime number: '))
> if not aa(a):
>     print "That's not a prime."
>     raise SystemExit
> a = (2**a)-1
> if aa(a):
>     print "It's a mersenne prime."
> else:
>     print "It's not a mersenne prime."
> #----
>
>
>
>
> #Check_Mersenne_Numbers.py
> def isprime(candidate):
>     for number in range(2,candidate):
>        if number % candidate == 0:
>           return False
>     return True
>
> power = int(raw_input('Please enter a prime number: '))
> if not isprime(power):
>     print "That's not a prime."
>     raise SystemExit
>
> mersenne_number = (2**power) - 1
>
> if not isprime(mersenne_number):
>     print "It's not a mersenne prime."
> else:
>     print "It is a mersenne prime."
> # ----
>
> Obviously, the whole aa(a) thing was designed to look ridiculous,
> but you have to remember that other people looking at your code don't
> know anything about it, like you do.
> You can comment the example #1 to the point where people can figure out
> what's going on,
> but example #2 is more or less completely clear as to what is happening
> at every step without any need for documentation.
> If you find yourself commenting "Remember here that file is no longer
> the input file but is now the output file"
> chances are you'd be better off having 2 variables, infile and outfile.
> If you fall into this pattern of willy-nilly naming variables you might
> someday have a script that looks like this:
>
> import os, sys
> print sys.argv
> sys = "Intel Core 2 Duo"
> os = "Windows XP Professional"
> print "This computer is running %s on a(n) %s" % (os,sys) #remember os
> and sys are strings and not modules
>
> #we need to use the os and sys modules again so we need to reimport them,
> #but we'll go ahead and import them as something else so we don't
> overwrite our string vars.
> import os as Os
> import sys as Sys
> if sys in Sys.argv: print "They passed ",sys," as an argument!"
> #we need an alternate configuration now
> SYs = "AMD ATHLON 64"
> OS = "Ubuntu"
> #let's get the name of this script
> SYs = Os.path.split(Sys.argv[0])[-1]
> sys = "Error creating directory."
> print "this script is called %s" % SYs
> #subdirectories for our oses
> try:
>     Os.mkdir(OS)
>     Os.mkdir(os)
> except:
>     print sys
>
> #------
> Haha.
> Just be forewarned!  Coming up with good variable names is extremely
> important!
>
> > My naming was purely for my ease of mind.. I personally care less
> > about what I call it when I declare, as to how it logically flows when
> > I go to use it. I'm sure this is considered poor method, but once I
> > declare a method I tend to never need to change the declaration, just
> > how I use the info... I hope that makes sense.
> No, I don't know what you mean here.
> >
> > On 10/16/06, *Kent Johnson* <kent37 at tds.net <mailto:kent37 at tds.net>>
> > wrote:
> >
> >     Chris Hengge wrote:
> >     > Here is my solution, completed with (I think) all your
> >     suggestions...
> >     >
> >     >
> >
> #########################################################################
> >     > def extractZip(filePathName):
> >     >     """
> >     >     This method recieves the zip file name for decompression,
> >     placing the
> >     >     contents of the zip file appropriately.
> >     >     """
> >     >     if filePathName == "":
> >     >         print "No file provided...\n"
> >     >     else:
> >     >         try: # Attempt to unzip file.
> >     >             zFile = zipfile.ZipFile(filePathName.strip('"'), "r")
> >     >             for aFile in zFile.namelist(): # For every file in
> >     the zip.
> >     >                 # If the file ends with a needed extension,
> >     extract it.
> >     >                 for ext in ['.cap', '.hex', '.fru', '.cfg',
> '.sdr']:
> >     >                     if aFile.lower().endswith(ext):
> >     >                         insideZip = aFile # Copy of Filename.
> >     >                         if "/" in aFile: # Split the filename if
> >     '/'.
> >     >                           aFile = aFile.rsplit('/', 1)[-1]
> >     >                         elif  "\\" in aFile: # Split the
> >     filename if '\'.
> >     >                           aFile = aFile.rsplit('\\',
> >     > 1)[-1]
> >     >                         outfile = open( aFile.lower(), 'w') # Open
> >     > output buffer for writing.
> >     >                         outfile.write(zFile.read(insideZip)) #
> >     Write the
> >     > file.
> >     >                         outfile.close() # Close the output file
> >     buffer.
> >     >             print "Resource extraction completed successfully!\n"
> >     >         except IOerror, message: # If file creation fails, let
> >     the user
> >     > know.
> >     >             print "File could not be written: \n"
> >     >             print message
> >     >
> >     >
> >
> #########################################################################
> >     > Definatly an improvement! Thanks Kent.
> >
> >     Yes, that is what I meant. One minor quibble, I think I would keep
> >     aFile
> >       as the name in the zip, since that is what it starts as, and use
> >     a new
> >     name for the external file name. Maybe you could use better names,
> for
> >     example zipPath and fileName. I think that would make the code a
> >     little
> >     clearer but it is a very minor point.
> >
> >     Kent
> >
> >
> >
> > ------------------------------------------------------------------------
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061016/8663cc10/attachment-0001.html 


More information about the Tutor mailing list