Help with some python code? [Solved]

mcpmcp Registered User regular
edited February 2011 in Help / Advice Forum
I have this code:
for infile in glob.glob(os.path.join(path, '*')):
           out.add(infile)
           file.write('\File added:' + infile)

Out is a variable for the locations of a tarfile. I'm adding files to it. When complete, it adds folder hierarchy as well as the files. I just want to add the files so that when I untar it, I don't have to dig through a bunch of folders to get to the files. Can anyone clue me in on how to do that?

mcp on

Posts

  • DocDoc Registered User, ClubPA regular
    edited February 2011
    Try
    out.add(infile, arcname=os.path.basename(infile))
    
    Also just for python nit-picking, you generally should use string interpolation instead of the '+' operator:
    '\File added: %s' % infile
    

    Doc on
  • mcpmcp Registered User regular
    edited February 2011
    Awesome. That's what I was lookin' for.

    Thanks for the info on strings too. This is my first shot at doing something in python, and you've made my life a little easier.

    mcp on
  • BarrakkethBarrakketh Registered User regular
    edited February 2011
    Doc wrote: »
    Try
    out.add(infile, arcname=os.path.basename(infile))
    
    Also just for python nit-picking, you generally should use string interpolation instead of the '+' operator:
    '\File added: %s' % infile
    

    If he is using either Python 2.6+ or Python 3.x, then he shouldn't be using that. Instead he should be using the string's format method:
    "\File added: {}".format(infile)
    

    If no format specifier is given it defaults to a given object's str() representation. If you like you can use an int to specify a positional argument, or a string for a named argument:
    "\File added: {0}".format(infile)
    "\File added: {filename}".format(filename=infile)
    

    For more see the docs, particularly the sections Format String Syntax and Format Specification Mini-Language.

    Barrakketh on
    Rollers are red, chargers are blue....omae wa mou shindeiru
  • DocDoc Registered User, ClubPA regular
    edited February 2011
    Barrakketh wrote: »
    Doc wrote: »
    Try
    out.add(infile, arcname=os.path.basename(infile))
    
    Also just for python nit-picking, you generally should use string interpolation instead of the '+' operator:
    '\File added: %s' % infile
    

    If he is using either Python 2.6+ or Python 3.x, then he shouldn't be using that. Instead he should be using the string's format method:
    "\File added: {}".format(infile)
    

    If no format specifier is given it defaults to a given object's str() representation. If you like you can use an int to specify a positional argument, or a string for a named argument:
    "\File added: {0}".format(infile)
    "\File added: {filename}".format(filename=infile)
    

    For more see the docs, particularly the sections Format String Syntax and Format Specification Mini-Language.

    Yeah, this is probably the case for what you should do these days. Just about anything is better than 'foo' + bar, though.

    Anyway, PEP-8 is your friend, as well:
    http://www.python.org/dev/peps/pep-0008/

    Doc on
Sign In or Register to comment.