os.path pathname manipulations

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bouzy
    New Member
    • Jun 2007
    • 30

    os.path pathname manipulations

    Code:
    for dirpath,dirs,files in os.walk(cwd):
    for file in files:
    
    global z_path
    z_path = os.path.  # WHAT WOULD GO HERE #  (dirpath)   
    
    print z_path
    This is part of a script where I am tying to grab the names of folders and an undetermined number of sub-folders/files and print them to screen. I don't know how deep the sub-folders will go. Currently when I do this it prints things like

    C:\Documents and Settings\B\Desk top\Test\T1\tes t.txt.

    What I want is to go from cwd (in this case Test) and just print to screen

    \T1\test.txt

    I have tried many os.path pathname manipulators like...

    abspath( path)
    basename( path)
    commonprefix( list)
    dirname( path)
    split( path)
    splitdrive( path)
    splitunc( path)

    ...and none seem to work.

    I can't think of any way to do this with a url that will change so the url could also be...

    C:\Documents and Settings\B\Desk top\Test\T1\444 \test.txt.

    and I want it to go from test > test.txt or T1\444\test.txt ...

    Ignore whats below if it doesn't make sense.
    I thought of assigning a variable to cwd and then splitting the url based on that, but I don't know how to go about doing that specifically.

    Does anyone know how this could be done?
  • nishi2rock
    New Member
    • Nov 2008
    • 3

    #2
    I could not exactly understand your problem.
    According to my understanding you dont want the whole Filepath to be printed on the console.

    Try this code

    Code:
    Curdir = os.getcwd()
    
    for DirInfo in os.walk(Curdir):
        for File in DirInfo[2]:
           print (DirInfo[0]+"\\"+File).replace(Curdir,"")

    Comment

    Working...