4

I have a procedure that os.walks a directory and its subdirectories to filter pdf files, separating out their names and corresponding pathnames. The issue I am having is that it will scan the topmost directory and print the appropriate filename e.g. G:/Books/Title.Pdf but the second it scans a subfolder e.g G:/Books/Sub Folder/Title.pdf it will print the following

G:/Books/Sub Folder\\Title.Pdf

(which is obviously an invalid path name). It will also add \\ to any subfolders within subfolders.

Below is the procedure:

def dicitonary_list():
    indexlist=[]        #holds all files in the given directory including subfolders
    pdf_filenames=[]    #holds list of all pdf filenames in indexlist
    pdf_dir_list = []   #holds path names to indvidual pdf files 

    for root, dirs,files in os.walk('G:/Books/'):
        for name in files:
            indexlist.append(root + name)
            if ".pdf" in name[-5:]:
                pdf_filenames.append(name)

    for files in indexlist:
        if ".pdf" in files[-5:]:
            pdf_dir_list.append(files)

    dictionary=dict(zip(pdf_filenames, pdf_dir_list))       #maps the pdf names to their directory address

I know it's something simple that I am missing but for love nor money can i see what it is. A fresh pair of eyes would help greatly!

tripleee
  • 175,061
  • 34
  • 275
  • 318
Robert Lear
  • 91
  • 1
  • 3
  • 7
  • 3
    `G:/Books/Sub Folder\\Title.Pdf` is a perfectly valid path name, if the `\\ ` is to be interpreted as Python string notation for a single `\ `. How are you printing the path? – Fred Foo Sep 05 '12 at 19:48
  • 4
    Instead of `indexlist.append(root + name)` you should use `indexlist.append(os.path.join(root,name))` – halex Sep 05 '12 at 20:01
  • 1
    `os.path.sep` will tell you the correct separator to use for the OS you are on ... there are lots of os.path stuffs that can help this kind of thing... – Joran Beasley Sep 05 '12 at 20:42

1 Answers1

12

Forward slashes and backward slashes are both perfectly valid path separators in Python on Windows.

>>> import os
>>> os.getcwd()
'j:\\RpmV'
>>> os.path.exists('j:\\Rpmv\\make.py')
True
>>> os.path.exists('j:/rpmv/make.py')
True
>>> os.path.isfile('j:\\Rpmv/make.py')
True
g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
  • No problem figured out what was wrong I didn't explain myself as well as i should have but understanding that the forward or backslash doesnt matter helped. so converted the forward slash to double backslash to escape in os.walk and then using os.start to execute the pathname to load the file worked thanks again – Robert Lear Sep 05 '12 at 20:54