473,498 Members | 1,930 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 2744
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
"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
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
"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('.').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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2664
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...
5
2193
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 =...
39
2610
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...
3
15429
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...
1
3503
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...
1
2897
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...
5
1084
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...
0
7125
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
7002
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...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4908
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...
0
3093
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...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
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 ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.