473,563 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5680
On 10/04/2008, Soren <so************ ****@gmail.comw rote:
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('pat hname') if os.path.isfile( f) ]

cheers
Dave
Apr 10 '08 #2
On 10 Apr, 11:55, Soren <soren.skou.nie l...@gmail.comw rote:
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(pa th, filename))]

--
Gabriel Genellina

Apr 10 '08 #4
On Apr 10, 12:14 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Thu, 10 Apr 2008 06:55:13 -0300, Soren <soren.skou.nie l...@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(pa th, 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(di rname)).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(di rname)).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
1973
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
3723
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
1445
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, "keywords" on the installed drives etc? -- Michael Bragg, President eSolTec, Inc. a 501(C)(3) organization MS Authorized MAR
4
2103
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 to the list, then the filenames added to another separate but connected list within each directory node. So far I have the program working in...
4
4203
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 the 2 of them together. This is what I tried: Sub Page_Load(.....) Dim dInfo As DirectoryInfo dInfo = New DirectoryInfo(Server.MapPath(MyDir))
1
1678
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 'MyDir' directory. Both 'Dir1' & 'Dir2' also house sub-directories & files. Assume that the sub-directory 'Dir1' has 3 directories & 3 files. When a...
2
12295
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 of doing it for files, but for some reason you can't do the same to directoryinfo.
7
2182
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 can be one to three digits. Example directory: other.zip
5
5880
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 directories/files from server "A" to server "B" using php. I have control on both server but i want to run PHP script into Linux server "B". As i am not good in...
0
7664
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7885
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.