os.listdir(<file specifications>) doesn't work ??

S

Stef Mientki

hello,

I want to find all files with the extension "*.txt".
From the examples in "Learning Python, Lutz and Asher" and
from the website I see examples where you also may specify a wildcard filegroup.

But when I try this
files = os.listdir('D:\\akto_yk\\yk_controle\\*.txt')

I get an error message

WindowsError: [Errno 123] The filename, directory name, or volume label syntax is incorrect:
'D:\\akto_yk\\yk_controle\\*.txt/*.*'

What am I doing wrong ?

thanks,
Stef Mientki
 
T

timw.google

hello,

I want to find all files with the extension "*.txt".
From the examples in "Learning Python, Lutz and Asher" and
from the website I see examples where you also may specify a wildcard filegroup.

But when I try this
files = os.listdir('D:\\akto_yk\\yk_controle\\*.txt')

I get an error message

WindowsError: [Errno 123] The filename, directory name, or volume label syntax is incorrect:
'D:\\akto_yk\\yk_controle\\*.txt/*.*'

What am I doing wrong ?

thanks,
Stef Mientki

You want the glob module

http://docs.python.org/lib/module-glob.html

import glob
glob.glob('*.txt')
 
S

Stef Mientki

timw.google said:
hello,

I want to find all files with the extension "*.txt".
From the examples in "Learning Python, Lutz and Asher" and
from the website I see examples where you also may specify a wildcard filegroup.

But when I try this
files = os.listdir('D:\\akto_yk\\yk_controle\\*.txt')

I get an error message

WindowsError: [Errno 123] The filename, directory name, or volume label syntax is incorrect:
'D:\\akto_yk\\yk_controle\\*.txt/*.*'

What am I doing wrong ?

thanks,
Stef Mientki

You want the glob module

http://docs.python.org/lib/module-glob.html

import glob
glob.glob('*.txt')

thanks that works !
Still don't know why it's not allowed through listdir ;-)

cheers,
Stef Mientki
 
G

Gabriel Genellina

En Mon, 14 May 2007 18:24:23 -0300, Stef Mientki
files = os.listdir('D:\\akto_yk\\yk_controle\\*.txt')

I get an error message

WindowsError: [Errno 123] The filename, directory name, or volume
label syntax is incorrect:
'D:\\akto_yk\\yk_controle\\*.txt/*.*'
Still don't know why it's not allowed through listdir ;-)

Because listir expects a single directory as parameter, not a wildcard
specification.
That is, you could do:
files = os.listdir("D:\\akto_yk\\yk_controle")
to get ALL the filenames in that directory, and then filter them using
fnmatch. But that's exactly what the glob module does internally.
 
S

Stef Mientki

Michel said:
Hi!



Warning: glob has "unix like behavior"; just a little different with
windows's DIR

Don't know the details of "Unix"
but I thought "unix" was case-sensitive,
and glob.glob doesn't seem to be,
at least not on windows systems ;-)

cheers,
Stef Mientki
 
L

Looney, James B

The case sensitivity has to do with the OS you're on. So, using glob
from Un*x is case sensitive, but from Windows it isn't.


-----Original Message-----
From: [email protected]
[mailto:p[email protected]] On
Behalf Of Stef Mientki
Sent: Monday, May 14, 2007 3:39 PM
To: (e-mail address removed)
Subject: Re: os.listdir(<file specifications>) doesn't work ??

Michel said:
Hi!



Warning: glob has "unix like behavior"; just a little different with
windows's DIR

Don't know the details of "Unix"
but I thought "unix" was case-sensitive,
and glob.glob doesn't seem to be,
at least not on windows systems ;-)

cheers,
Stef Mientki
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top