#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    7
    Rep Power
    0

    Red face Searching for files :(


    import glob
    for files in glob.glob('*.txt')
    Print "I Found" + Files
    b= raw_input("")

    That should list files in current dir all *.txt?

    Is there a better way of doing this? with erm the (dir) command? anything?

    Please, please, please make me a simple working example im using python 2.4

    Help thnx
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    If you don't care about specific file types or names then you can use the listdir() or walk() functions from the os module. Here's an example that uses listdir() for get a list of all the files in the current working directory:

    Code:
    >>> import os
    >>> for pathName in os.listdir('/'):
    ...     print 'Found', `pathName`
    ...
    Found 'bin'
    Found 'dev'
    Found 'etc'
    Found 'lib'
    Found 'mnt'
    Found 'opt'
    Found 'srv'
    Found 'tmp'
    Found 'sys'
    Found 'var'
    Found 'usr'
    Found 'boot'
    Found 'home'
    Found 'proc'
    Found 'sbin'
    Found 'root'
    Found 'media'
    Found 'windows'
    >>>
    The reason I've opted for this rather than walk() is simply because it is easier to work with, and unless you need to walk a directory tree is usually overkill .

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    import glob
    for files in glob.glob('*.txt')
    Print "I Found" + Files
    b= raw_input("")

    That should list files in current dir all *.txt?

    Is there a better way of doing this?
    There are other ways, as netytan says, but what's wrong with glob?

    Apart from your code printing each filename a few times...

    Code:
    import glob
    
    for text_file in glob.glob("*.txt"):
        print "I found", text_file
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    7
    Rep Power
    0
    ThaX alot
    sorry i mean other ways of doing it not better ways of doing

    thx again

    Im 16 in 1 hour w00t
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    No problem Sara. Anyway, sbf what did you mean when you said that the code prints each filename a few times; besides several knowticable errors theres nothing that I would expect to cause anything like that?

    BTW happy birthday ,

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    *blinks*

    er, I meant I'm an idiot. ...

    I read "files" as being a list of all the files, just because it's written as a plural. *ahem*

    [Yes, Happy Birthday too]

IMN logo majestic logo threadwatch logo seochat tools logo