473,386 Members | 1,830 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.

How can I get the names of the files in a directory?

Can you guys also help me find a module that looks in
a directory and print out the names of the files in there?

__________________________________
Do you Yahoo!?
Yahoo! Mail - Helps protect you from nasty viruses.
http://promotions.yahoo.com/new_mail
Jul 18 '05 #1
3 1250
Sara Fwd said the following on 1/15/2005 8:10 AM:
Can you guys also help me find a module that looks in
a directory and print out the names of the files in there?


You can use glob:
import glob
from os.path import isfile
print filter(isfile, glob.glob('/tmp/*')) # can use patterns


(will print a list of all files in the given directory, matching the
given pattern)

If you want to traverse a directory tree recursively, please take a look
at this recipe:
http://aspn.activestate.com/ASPN/Coo.../Recipe/200131

Thanks,
--Kartic
Jul 18 '05 #2
On Sat, 15 Jan 2005 15:16:02 GMT, .removethis.
<"(.removethis.)kartic.krishnamurthy"@gmail.com > wrote:
Sara Fwd said the following on 1/15/2005 8:10 AM:
Can you guys also help me find a module that looks in
a directory and print out the names of the files in there?


You can use glob:
>>> import glob
>>> from os.path import isfile
>>> print filter(isfile, glob.glob('/tmp/*')) # can use patterns


(will print a list of all files in the given directory, matching the
given pattern)

If you want to traverse a directory tree recursively, please take a look
at this recipe:
http://aspn.activestate.com/ASPN/Coo.../Recipe/200131


import os, os.path

def get_all_files(path):
if len(path) > 0:
if path[-1] == ':':
path = path + '\\'
try:
for i in os.listdir(path):
j = os.path.join(path, i)
if os.path.isdir(j):
for ii in get_all_files(j):
yield ii
else:
yield j
except:pass

for i in get_all_files('c:\\'):
print i
Jul 18 '05 #3

På 15. jan 2005 kl. 16:16 skrev .removethis.:
import glob
from os.path import isfile
print filter(isfile, glob.glob('/tmp/*')) # can use patterns


Nice example of when filter() is better than list comprehension.

[f for f in glob.glob("/tmp/*") if isfile(fi)]
is a bit too verbose, the iteration is a totally uninteresting part
here.

--
Stian Søiland
Trondheim, Norway
http://www.soiland.no/
=/\=

Jul 18 '05 #4

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

Similar topics

2
by: Sheila | last post by:
I am hoping someone here has experience with this. I am using Access 97, but if someone has done this in another version I'd appreciate input as well I need to populate a table with subdirectory...
5
by: Bas Hendriks | last post by:
Has anyone any idea how asp.net find it's files back after compiling them to the temporary asp.net directory? I found on numerous webpages that the directorynames are chosen random but cannot find...
6
by: Mark | last post by:
I want the VB.Net program to read and file names from a directory. I thought the best thing to do was to place these file names into a "temporary" MS Access database for sorting, etc. So I'm...
6
by: jyoti | last post by:
hi, We have a requirement wherein we have to find the names of the files/size present in the https server and Download the files based on certain size /naming criteria. Please help us out in...
22
by: rtilley | last post by:
# Spaces are present before and after the XXX filename = ' XXX ' new_filename = filename.strip() if new_filename != filename: print filename Macs allow these spaces in file and folder...
3
by: jpabich | last post by:
I want to display a list of filenames that exist in a certain directory. How do I go about loading this list?
3
by: cjb | last post by:
Is there a way to get Directory.GetFiles to return multi-language file names? I haven't found any overloads that allow any such parameter. The way I am using it now is: foreach (string d in...
2
by: xeshu | last post by:
HelowWw fellow programmers :) I have a web application that opens a pdf file. Very simple indeed. However the file name is not known. The file has to be found by first searching the list of all...
1
by: mujunshan | last post by:
Yesterday, I installed PythonCE on my cellphone whose OS is Windows Mobile 5.I wanted to use numpy as calculation tool.But after I copy numpy module in my desktop computer into my phone,I find many...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.