473,698 Members | 2,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1261
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.krishna murthy"@gmail.c om> 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(p ath):
if len(path) > 0:
if path[-1] == ':':
path = path + '\\'
try:
for i in os.listdir(path ):
j = os.path.join(pa th, 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
1846
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 names. Example: c:\Tests has the subdirectories test1, test2 and test3. Each directory has a file called test1.txt, test2.txt, test3.txt. I would like to populate a table with just the subdirectory names.
5
3853
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 how asp.net find's it's compiled files when asking for the same webpage which was complied before. -- Bas Hendriks Technical Specialist PinkRoccade
6
24448
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 looking for sample code that will instantiate MS Access (one that only exists during the execution of the VB.Net program) and read and input these file names from a directory into the Access table. Thanks,
6
1555
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 getting the names/size of the files in the server. Thanks
22
4963
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 names. Which is OK. The problem arises when the file or folder is copied to a PC running Windows
3
1790
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
4145
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 Directory.GetDirectories("C:\\")) { foreach (string f in Directory.GetFiles(d, "*.exe")) { MessageBox.Show(f); } }
2
3833
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 files present in a particular directory. All i want to know is how do i get a list of the file names in that directory. Lets say the directory is LBS and this directory contains a number of pdf files. All i want to do is get their names. Also...
1
2520
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 file names in directory \numpy were changed into capital letters.For example: __init__.py was chaged into __INIT__.PY However PythonCE is sensitive to capital directory or filenames. So it is boring to convert those file names to into...
0
8678
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9166
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6525
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.