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

How to separate directory list and file list?

P: n/a
With
glob.glob("*")
oros.listdir(cwd)


I can get a combined file list with directory list, but I just wanna a
bare file list, no directory list. How to get it?

Tons of thanks in advance!

Gonnasi
Oct 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi,

2005/10/23, Gonnasi <ha******@21cn.net>:
With
glob.glob("*")


or
os.listdir(cwd)


I can get a combined file list with directory list, but I just wanna a
bare file list, no directory list. How to get it?


don't know if it is the best solution, but it looks nice. :)

path = "/home/test"
files = [fn for fn in os.listdir(path) if
os.path.isfile(os.path.join(path, fn))]

This gives you just the list of files in a given directory.

Best regards,
Oliver

--
Oliver Andrich <ol************@gmail.com> --- http://roughbook.de/
Oct 23 '05 #2

P: n/a
"Gonnasi" wrote:
With
glob.glob("*")


or
os.listdir(cwd)


I can get a combined file list with directory list, but I just wanna a
bare file list, no directory list. How to get it?


use os.path.isfile on the result.

for file in glob.glob("*"):
if not os.path.isfile(file):
continue
... deal with file ...

for file in os.listdir(cwd):
file = os.path.join(cwd, file)
if not os.path.isfile(file):
continue
... deal with file ...

files = map(os.path.isfile, glob.glob("*"))

files = (file for file in os.listdir(cwd) if os.path.isfile(os.path.join(cwd, file)))

etc.

</F>

Oct 23 '05 #3

P: n/a
Gonnasi wrote:
With
glob.glob("*")

or
os.listdir(cwd)


I can get a combined file list with directory list, but I just wanna a
bare file list, no directory list. How to get it?


Using Jason Orendorff's path module, it's just this:
from path import path
path('.').files() # returns list of files in current dir

-Peter
Oct 23 '05 #4

P: n/a
"Gonnasi" <ha******@21cn.net> wrote:
With
glob.glob("*")


or
os.listdir(cwd)


I can get a combined file list with directory list, but I just wanna a
bare file list, no directory list. How to get it?

Tons of thanks in advance!

Gonnasi


Using the path module (http://www.jorendorff.com/articles/python/path/):

from path import path
path('.').files()

George
Oct 23 '05 #5

P: n/a
Gonnasi <ha******@21cn.net> wrote:
With
glob.glob("*")


or
os.listdir(cwd)


I can get a combined file list with directory list, but I just wanna a
bare file list, no directory list. How to get it?


I see everybody's suggesting os.path.* solutions, and they're fine, but
an interesting alternative is os.walk:

__, thedirs, thefiles = os.walk('.').next()

thefiles is the list of filenames (and thedirs is the list of directory
names), and each is sorted alphabetically. (I'm assigning to '__' the
absolute path of the current directory, meaning I intend to ignore it).
An expression that just provides the filename list is

os.walk('.').next()[2]

although this may be a tad too obscure to recommend it!-)
Alex
Oct 23 '05 #6

P: n/a
Lots of thanks for your help, My code can return the right result now.

Thanks again!

On Sun, 23 Oct 2005 17:27:49 +0200, "Fredrik Lundh"
<fr*****@pythonware.com> wrote:
"Gonnasi" wrote:
With
>glob.glob("*")


or
>os.listdir(cwd)


I can get a combined file list with directory list, but I just wanna a
bare file list, no directory list. How to get it?


use os.path.isfile on the result.

for file in glob.glob("*"):
if not os.path.isfile(file):
continue
... deal with file ...

for file in os.listdir(cwd):
file = os.path.join(cwd, file)
if not os.path.isfile(file):
continue
... deal with file ...

files = map(os.path.isfile, glob.glob("*"))

files = (file for file in os.listdir(cwd) if os.path.isfile(os.path.join(cwd, file)))

etc.

</F>


Oct 24 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.