473,385 Members | 1,356 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,385 software developers and data experts.

Get the filenames in a directory and its subdirectories

That would be very handy for a small progrtam I've written. I've
browsed through the documentation for the standard modules but
there doesn't seem to be a way (please correct me if I'm wrong).

I know that it shouldn't be very hard to write a module myself
using listdir() and isdir() or stuff like that, but I'm having
problems.
Jul 18 '05 #1
3 4376
> That would be very handy for a small progrtam I've written. I've
browsed through the documentation for the standard modules but
there doesn't seem to be a way (please correct me if I'm wrong).

I know that it shouldn't be very hard to write a module myself
using listdir() and isdir() or stuff like that, but I'm having
problems.


There are a my funcs (for Windows):
===========================================
def rec_glob(path,mask)
def rec_glob(mask)
===========================================
import os,fnmatch

def rec_glob_get_dirs(path):
try:
for i in os.listdir(path):
if os.path.isdir(path+i):
yield os.path.basename(i)
except:pass
def rec_glob(path,mask):
p=[]
if len(path)<>0:
if path[-1]!='\\':
path=path+'\\'
for i in rec_glob_get_dirs(path):
for ii in rec_glob(path+i,mask):
yield ii
try:
for i in os.listdir(path):
ii=i
i=path+i
if os.path.isfile(i):
if fnmatch.fnmatch(ii,mask):
yield i
except:pass
def rec_glob(mask):
rec_glob('',mask)

if __name__ == '__main__':
f=open('log','wb')
for i in rec_glob('E:','*'):
f.write(i+'\n')
f.close()
print 'Done.'
#while 1:pass
===========================================

and

===========================================
import os,fnmatch

def rec_glob_get_dirs(path):
d=[]
try:
for i in os.listdir(path):
if os.path.isdir(path+i):
d.append(os.path.basename(i))
except:pass
return d
def rec_glob(path,mask):
l=[]
if len(path)<>0:
if path[-1]!='\\':
path=path+'\\'
for i in rec_glob_get_dirs(path):
l=l+rec_glob(path+i,mask)
try:
for i in os.listdir(path):
ii=i
i=path+i
if os.path.isfile(i):
if fnmatch.fnmatch(ii,mask):
l.append(i)
except:pass
return l

def rec_glob(mask):
rec_glob('',mask)

if __name__ == '__main__':
f=open('log','wb')
for i in rec_glob('C:\\','*'):
f.write(i+'\n')
f.close()
print 'Done.'
#while 1:pass
===========================================

Jul 18 '05 #2
"Psymaster" wrote:
That would be very handy for a small progrtam I've written. I've
browsed through the documentation for the standard modules but
there doesn't seem to be a way (please correct me if I'm wrong).
os.path.walk (with a rather weird interface; look it up in the docs)
I know that it shouldn't be very hard to write a module myself
using listdir() and isdir() or stuff like that, but I'm having problems.


here's a simple version; if you want, you can change it into
a generator (use yield to return the names, instead of adding
them to one big list), but it's perfectly usable as is [1]:

def listdir(root, path=""):
# recursive listdir
files = []
try:
for file in os.listdir(os.path.join(root, path)):
pathname = os.path.join(path, file)
if os.path.isdir(os.path.join(root, pathname)):
files.extend(listdir(root, pathname))
else:
files.append(pathname)
except OSError:
pass
return files

you may also want to take a look at the os.path examples in
my library book:

http://effbot.org/zone/librarybook-index.htm
=> core modules, page 1-35ff

</F>

1) unless I messed something up when posting it, of course.


Jul 18 '05 #3
"Fredrik Lundh" <fr*****@pythonware.com> wrote on Sun, 29 Jun
2003 21:23:13 GMT in comp.lang.python:
That would be very handy for a small progrtam I've
written. I've browsed through the documentation for the
standard modules but there doesn't seem to be a way
(please correct me if I'm wrong).


os.path.walk (with a rather weird interface; look it up in
the docs)


Yes, I've now understood ot, but it isn't only cryptic itself,
its documentation is even cryptier.

Anyway.
Jul 18 '05 #4

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

Similar topics

5
by: Anand K Rayudu | last post by:
Hi all, I am trying to find a way to get the files recursively in a given directory, The following code is failing, can some one please suggest what could be problem here from os import...
1
by: Thieum22 | last post by:
Hi, I try to go through a directory and it's subdirectories to reah the properties of each files. But I have a problem to set active the directory where the files are, in order to display their...
10
by: Julie | last post by:
Why does the faq make this statement regarding slashes in paths: "This is because the library routines on these operating systems handle "/" and "\" interchangeably. " ...
8
by: James Owens | last post by:
I'm a relative newbie, interested in storing the information from several server directories and subdirectories in XML so that I can present it selectively using XSL (all files updated today or...
6
by: Hemant Shah | last post by:
Folks, I need to move HOME directory of an instance to another directory. What is the best way of doing it? Is changing password file enough? or dies DB2 store this info in it's own config? ...
0
by: Dennis | last post by:
Expanding a basic recursive method I got from this news group for getting all files in a directory and it's subdirectories, I thought someone might be interested in this (it's pretty simple but...
1
by: Thomas Smith via .NET 247 | last post by:
Hi I am trying to write some code which allow me to specify a toplevel (parent) directory. Then the program needs to be able toshow every filename (fullpath) in all subdirectories. It shouldinclude...
9
by: smoothoperator12 | last post by:
Hi all, I have spent hours trying to figure out where I have went wrong with my code for my recursive function to list all the files in a directory, and all of the files in all of its...
8
by: theCancerus | last post by:
Hi All, I am not sure if this is the right place to ask this question but i am very sure you may have faced this problem, i have already found some post related to this but not the answer i am...
1
by: Rinaldo | last post by:
Hi, I have a problem to get the directory. I've tryed: public void WriteWeb(DirectoryInfo directory) {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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...

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.