January 10th, 2005, 03:42 PM
Searching for files :(
for files in glob.glob('*.txt')
Print "I Found" + Files
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
January 10th, 2005, 04:11 PM
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:
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 .
>>> import os
>>> for pathName in os.listdir('/'):
... print 'Found', `pathName`
Hope this helps,
January 10th, 2005, 05:59 PM
There are other ways, as netytan says, but what's wrong with glob?
Apart from your code printing each filename a few times...
for text_file in glob.glob("*.txt"):
print "I found", text_file
January 10th, 2005, 06:06 PM
sorry i mean other ways of doing it not better ways of doing
Im 16 in 1 hour w00t
January 10th, 2005, 06:28 PM
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 ,
January 10th, 2005, 08:18 PM
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]