File Path retrieving problem

music24by7 at gmail.com music24by7 at gmail.com
Thu Feb 26 04:20:17 EST 2009


Hi Dennis Thanks for your reply and also the detailed explanation.

>         Or do you mean you want something that, given a bare name, searches
> your file system to find where a file with that name actually is?
>

Yes, this is what i exactly needed.
I have found something interesting to do this using the os.walk(path)
function, and it worked....
I am pasting the code which i used for this:



testpath=r"C:\\"
logpath=r"C:\Pathfinder.txt"
csvpath=r"C:\Pathfinder.csv"
import os, csv

def logData(d={}, logfile="c://filename999.txt", separator="\n"):
    """Takes a dictionary of values and writes them to the provided
file path"""
    logstring=separator.join([str(key)+":  "+d[key] for key in
d.keys()])+"\n"
    f=open(logfile,'a')
    f.write(logstring)
    f.close()
    return
def walker(topPath,csvpath):
    fDict={}
    logDict={}
    limit=1000
    freed_space=0
    items=0
    fileHandle = csv.writer(open(csvpath, 'w'))
    fileHandle.writerow(['Index','Path'])
    for root, dirs, files in os.walk(topPath):
        for name in files:
            fpath=os.path.join(root,name)
            logDict["Name"]=name
            logDict["Path"]=fpath
            if name.find("NEWS"):
                continue
            else:
            logData(logDict, logpath, "\t")
            fDict[items] = name
            items=len(fDict.keys())
            fileHandle.writerow([items,fpath])
            print "Dict entry:  ",items,
            print "Path:  ",fpath
            if items > limit:
                break
        if items > limit:
                break
walker(testpath,csvpath)



Finally thank you all very much for the patience to listen to me.


Regards,
Sudhir




More information about the Python-list mailing list