473,549 Members | 2,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to separate directory list and file list?

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
6 2749
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(pa th, 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
"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(cw d, file)
if not os.path.isfile( file):
continue
... deal with file ...

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

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

etc.

</F>

Oct 23 '05 #3
Gonnasi wrote:
With
glob.glob("*" )

or
os.listdir(cw d)


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
"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
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('.').ne xt()

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('.').ne xt()[2]

although this may be a tad too obscure to recommend it!-)
Alex
Oct 23 '05 #6
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*****@python ware.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(cw d, file)
if not os.path.isfile( file):
continue
... deal with file ...

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

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

etc.

</F>


Oct 24 '05 #7

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

Similar topics

0
2681
by: Tony Burger | last post by:
I'm trying to drag and drop files from a file list box to a directory list box (the two controls are not related). Does anyone have an examples of this? I've got the properties setup correctly on the two controls and I'm starting the OLEDrag method of the file list box using the mouse down event. This calls the OLEStartDrag where I am...
5
2198
by: bart plessers | last post by:
Hello, Currently I am developping a internet "directory browser" My page 'default.asp' has following entries: CurrentPATH = Request("MyLink") Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFolder = oFSO.GetFolder(CurrentPATH) Set oFolderContents = oFolder.Files
39
2614
by: Joe Laughlin | last post by:
If I have a character array with "/some/file/directory/file_name", are there any functions / libraries that I could use to separate the directory ("some/file/directory") from the file name ("file_name"). I looked at sscanf(), but that didn't seem to do what I wanted. Thanks, Joe
3
15438
by: Patrick Johnson | last post by:
Under visual basic there used to be a directory tree list box and file list box controls that could be placed on a form and linked together. Is there anything similar for C# or VB.net, I couldn't find anything...
1
3511
by: Steve | last post by:
Hi, I want to get a list of files in a directory, but I want to filter the list to *.vb, for example. If I use a straight forward Directory.GetFileSystemEntries(path, "*.vb") I get the filenames but also the path. I can use FileInfo with DirectoryInfo to just get the Filenames, but I get
1
2902
by: Strider | last post by:
Hi, A bit of a PHP newbie here who could do with some help. I have searched through many forums and resources trying to find a solution for a problem I am trying to resolve. I wish to populate a drop down menu with a list of files that sit remotely on a server. Basically the user uploads media files to a server directory via FTP and then...
5
1085
by: darren | last post by:
Hello In a program i'm writing, I need to be able to serve files from a specified directory. I don't want to reinvent the wheel by writing a class that provides basic directory a file functionality if something similar already exists. I'm looking for something similar to what;s provided in java i guess. The ability to list files in a...
0
7472
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
7986
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...
0
6074
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5391
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
5114
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
3518
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
1965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1083
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
786
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.