473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Sorting Directories from files in a os.listdir??

Hi,

I'd like to read the filenames in a directory, but not the
subdirectories, os.listdir() gives me everything... how do I separate
the directory names from the filenames? Is there another way of doing
this?

Thanks!
Apr 10 '08 #1
6 5665
On 10/04/2008, Soren <so****************@gmail.comwrote:
Hi,

I'd like to read the filenames in a directory, but not the
subdirectories, os.listdir() gives me everything... how do I separate
the directory names from the filenames? Is there another way of doing
this?
The only thing I can think of if you just want the immediate dir is to
use the os.path module's function isfile to test each item from the
list returned by os.listdir.

This should do the trick I think:

[ f for f in os.listdir('pathname') if os.path.isfile(f) ]

cheers
Dave
Apr 10 '08 #2
On 10 Apr, 11:55, Soren <soren.skou.niel...@gmail.comwrote:
Hi,

I'd like to read the filenames in a directory, but not the
subdirectories, os.listdir() gives me everything... how do I separate
the directory names from the filenames? Is there another way of doing
this?

Thanks!
I guess you have no other way than using os.path.isfile or
os.path.isdir for every entry returned by os.listdir.
--- Giampaolo
http://code.google.com/p/pyftpdlib
Apr 10 '08 #3
En Thu, 10 Apr 2008 06:55:13 -0300, Soren <so****************@gmail.com>
escribió:
I'd like to read the filenames in a directory, but not the
subdirectories, os.listdir() gives me everything... how do I separate
the directory names from the filenames? Is there another way of doing
this?
Check each returned name using os.path.isfile
(untested):

def files_only(path):
return [filename for filename in os.listdir(path)
if os.path.isfile(os.path.join(path, filename))]

--
Gabriel Genellina

Apr 10 '08 #4
On Apr 10, 12:14 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Thu, 10 Apr 2008 06:55:13 -0300, Soren <soren.skou.niel...@gmail.com>
escribió:
I'd like to read the filenames in a directory, but not the
subdirectories, os.listdir() gives me everything... how do I separate
the directory names from the filenames? Is there another way of doing
this?

Check each returned name using os.path.isfile
(untested):

def files_only(path):
return [filename for filename in os.listdir(path)
if os.path.isfile(os.path.join(path, filename))]

--
Gabriel Genellina
Thanks everyone! That worked! :)
Apr 10 '08 #5
Soren wrote:
Hi,

I'd like to read the filenames in a directory, but not the
subdirectories, os.listdir() gives me everything... how do I separate
the directory names from the filenames? Is there another way of doing
this?

Thanks!
import os
base, files, dirs = iter(os.walk(dirname)).next()
# now files is files and dirs is directories (and base == dirname)
Apr 10 '08 #6
Scott David Daniels wrote:
>I'd like to read the filenames in a directory, but not the
subdirectories, os.listdir() gives me everything... how do I separate
the directory names from the filenames? Is there another way of doing
this?

Thanks!

import os
base, files, dirs = iter(os.walk(dirname)).next()
# now files is files and dirs is directories (and base == dirname)
>>import os
os.listdir(".")
['fifo', 'dir', 'file'] # a fifo, a directory, and a file
>>w = os.walk(".")
w is iter(w) # calling iter() is a noop here
True
>>w.next()
('.', ['dir'], ['fifo', 'file']) # base, dirs, files; everything that is not
# a directory goes into the files list

Peter
Apr 10 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tum | last post by:
Hi folks, I've been trying to make a decision and it's driving me crazy. Is a directory a file or is a directory NOT a file but a node? Should I have A)
3
by: vishnu mahendra | last post by:
hello to all, Can any one please tell me how to delete a record in a binary file. thank you in advance, vishnu
5
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. I'm looking for a way to programmatically scan all installed drives for programs that are installed. Can someone point me to code to look for say,...
4
by: thoseion | last post by:
Hi, I am trying to get a program working whereby directory and file names are read into a list. I have been given the original list structure - it appears that the directory names should be added...
4
by: rn5a | last post by:
I have a ListBox which should list all the files & directories that exist in a particular directory. The problem is I can get the ListBox to list either all the files or all the directories but not...
1
by: rn5a | last post by:
A ListBox lists all the folders & files existing in a directory named 'MyDir' on the server. Assume that the ListBox lists 2 directories - 'Dir1' & 'Dir2' i.e. these 2 directories reside in the...
2
by: Cable | last post by:
Hi, I've got a simple: DirectoryInfo dis = d.GetDirectories(); foreach (DirectoryInfo di in dis) The trouble is I can't find a way to sort or order the directoryinfo array, I've found a way...
7
by: gtb | last post by:
I wish to copy the highest version number of a file from directory \ \ \fileserver\D:\scripts to C:\scripts where the file names are of the form filename_MM.NN.SS.zip, where MM, NN, and SS...
5
kamill
by: kamill | last post by:
I have two server one is window server as "A" and another is Linux server as "B". I have a directory on server "A" which contains several files and sub directories. I want to copy...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.