Convert Tuple to String

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeff4567
    New Member
    • Mar 2010
    • 1

    Convert Tuple to String

    I've got an issue where i'm trying to change names of a bunch of files in python. I've scoured the necessary directory and added the files to a list, and now im trying to remove the last 9 charecters from the filename and replace them with something new. When I try to take the letters off using fname =fname[-:9] its throwing me an error.

    here's what i've got so far. Thanks for the help.

    Code:
    import os, sys, string
    
    start = 'start path'
    
    list = []
    new_list = []
    
    for dirs in os.walk(start):
        list.append(dirs)
        print list
    print 'done'
    
    for fname in list:
        if '.pyw' in fname:
            fname = fname[:-9]
        new_list.append(fname)
    print new_list
    Last edited by bvdet; Mar 24 '10, 06:33 PM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Please use code tags when posting code.

    What error did you receive? Often that will tell us what the problem is.

    Do not use the name "list" as an identifier. That will mask the built-in function list().

    Your problem is that os.walk() returns a generator object that yields tuples consisting of:

    directory
    list of subdirectories
    list of files

    Comment

    Working...