By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,036 Members | 1,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,036 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
May 14 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On May 14, 4:09 pm, Stef Mientki <S.Mientki-nos...@mailbox.kun.nl>
wrote:
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')
May 14 '07 #2

P: n/a
timw.google wrote:
On May 14, 4:09 pm, Stef Mientki <S.Mientki-nos...@mailbox.kun.nl>
wrote:
>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
May 14 '07 #3

P: n/a
Hi!

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


--
@-salutations

Michel Claveau
May 14 '07 #4

P: n/a
En Mon, 14 May 2007 18:24:23 -0300, Stef Mientki
<S.**************@mailbox.kun.nlescribió:
>> 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.

--
Gabriel Genellina

May 14 '07 #5

P: n/a
Michel Claveau wrote:
Hi!

>You want the glob module

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
May 14 '07 #6

P: n/a
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: py*****************************************@python .org
[mailto:py***************************************** @python.org] On
Behalf Of Stef Mientki
Sent: Monday, May 14, 2007 3:39 PM
To: py*********@python.org
Subject: Re: os.listdir(<file specifications>) doesn't work ??

Michel Claveau wrote:
Hi!

>You want the glob module

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
--
http://mail.python.org/mailman/listinfo/python-list
May 14 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.