[Tutor] folder and file list

Tommy Bell tommy at enkelthed.dk
Wed May 11 08:52:12 CEST 2011


On 11-05-2011 00:08, Jorge Romero wrote:
> Is there any special reason for deploying that functionality from 
> scratch by yourself? Can't you use os bulit-in module?
>
> Perhaps you can find this useful 
> http://docs.python.org/library/os.html#os.listdir. That way you don't 
> deal with OS peculiarities such as the one Brett Ritter pointed.
>
> On Tue, May 10, 2011 at 8:45 AM, Brett Ritter <swiftone at swiftone.org 
> <mailto:swiftone at swiftone.org>> wrote:
>
>     On Tue, May 10, 2011 at 8:17 AM, Tommy Bell <tommy at enkelthed.dk
>     <mailto:tommy at enkelthed.dk>> wrote:
>     > scandir('c:\tmp')
>
>     > this doesnt work, I know why - but i dont know how to fix it.
>     > The reason it doesnt work is because isfile requires a file, but
>     current contains a path.
>
>     Not quite.  Stick a "print path" as the first line in scandir.
>
>     Notice that it doesn't print out c:\tmp
>
>     The issue is that Windows (Well, DOS, back in the day) decided to use
>     backslashes as the path separator, where the rest of the world
>     (mostly) used slashes.  This meant that most programming languages use
>     backslashes to "escape" characters to have special meaning.  Putting
>     "\n" in a string puts in not an "n" but a newline character.  \t is a
>     tab.  This causes you  (and many other windows programmers) a little
>     bit of distress today, in many programming languages.
>
>     To have your string recognize that your backslash is an actual real
>     backslash you can escape it:
>      scandir('c:\\tmp')
>
>     After that your code should work fine (it runs on my system, but I'm
>     not on windows).
>
>     This filepath issue has many details you can look up or ask about, but
>     this should get you started.
>
>     --
>     Brett Ritter / SwiftOne
>     swiftone at swiftone.org <mailto:swiftone at swiftone.org>
>     _______________________________________________
>     Tutor maillist  - Tutor at python.org <mailto:Tutor at python.org>
>     To unsubscribe or change subscription options:
>     http://mail.python.org/mailman/listinfo/tutor
>
>
>
>
> -- 
> Jorge Romero
>
Thanks for the assistance. I've attempted to use glob, I actually did 
try with os.listdir, but then I had to combine the basename (as I 
understand listdir, is only returns a string with basename) with the 
path, and I have trouble getting that to work with calling it self when 
I ran into a folder, ie the recursivecalls

import os,glob
##use os.walk
def scand(path):
     for current in glob.glob(os.path.join(path,'*')):
         if os.path.isdir(current) == True:
             print "Folder: " + os.path.basename(current)
             scand(current)
         if os.path.isfile(current) == True:
             print "file: " + os.path.basename(current)

scand('c:\\Users\Tommy\\docs')

instead, with an idea of using os.walk whic I havent tried yet, I dont 
much mind limiting it to a windows system as I will be using this on my 
own machine, but I see the idea of not being exposed to OS relevant 
limitations.
The reason for attempting to do this manually is because I am looking at 
making a script that can help me manage my abundent number of articles 
and reports for my research in a sensible way - and I thought it would 
be a good way of learning python.

Ultimately the idea is to expand it into now only listing the folders 
and files, but writing this information to a file (or a database) with 
added info such as notes and comments on the article/report in a .txt 
file that shares the same name as the pdf, and which contains extra info

/Regards
Tommy

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110511/9efea366/attachment-0001.html>


More information about the Tutor mailing list