473,804 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

My function to recursively list all files in directory tree..

4 New Member
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 subdirectories.

This is the code:


Expand|Select|Wrap|Line Numbers
  1.  
  2. import os
  3. import sys
  4. import dircache
  5. import stat
  6. import string
  7.  
  8.  
  9.  
  10.  
  11. def dirrecur(currsub):
  12.  
  13.  
  14.         thispath = os.getcwd()
  15.  
  16.         print thispath
  17.         print "\n"
  18.  
  19.  
  20.         if(len(currsub) == 0):   #if len = 0, no subdirectories were found.
  21.  
  22.                 os.chdir('..')   #go back up one step.
  23.  
  24.         else:
  25.                 dlist = []
  26.  
  27.                 for z in range(0,len(currsub)):
  28.  
  29.  
  30.                         os.chdir(currsub[z])
  31.  
  32.                         print currsub[z]
  33.                         print "\n"
  34.  
  35.                         direntries = dircache.opendir('.') #open this directory
  36.  
  37.                         for j in range(0,len(direntries)):
  38.  
  39.                                 statresult = os.stat(direntries[j])
  40.  
  41.                                 if (stat.S_ISDIR(statresult.st_mode) != 0):
  42.  
  43.  
  44.                                         dlist.append(direntries[j])     #dlist is built here.
  45.                                         print direntries[j]
  46.  
  47.                                 else:
  48.  
  49.                                         print direntries[j]
  50.  
  51.                          dirrecur(dlist)   #recur again with full list of subdirectories.
  52.  
  53.  
  54.  
  55. #MAIN
  56.  
  57.  
  58. abspath = os.getcwd()
  59.  
  60. direntries = dircache.opendir(abspath)
  61.  
  62. dirlist = []
  63.  
  64.  
  65.  
  66. for i in range(0,len(direntries)):
  67.  
  68.  
  69.         statresult = os.stat(direntries[i])
  70.  
  71.         if (stat.S_ISDIR(statresult.st_mode) != 0): #if it's a directory file.
  72.  
  73.                 dirlist.append(direntries[i])
  74.                 print direntries[i]                 #print it out too.
  75.  
  76.         else:
  77.  
  78.                 print direntries[i]
  79.  
  80.  
  81.  
  82. dirrecur(dirlist)
  83.  
  84.  
  85.  


It goes down to level 3 of the directory tree, but at one point I get the
error 2, the no such file or directory message. I have checked the
file permissions of the directory that it gets stuck on, and I have no idea why
it is gettintg stuck on that directory and not one of the directories before it.

I would be extremely grateful if someone could lend another set of eyes to my
code and see where I'm going wrong at, and why it goes to level 3 so to speak
of the tree and then fails.

I'm stumped!


Thanks so much in advance. Any and all input would be greatly appreciated!
Jul 16 '07 #1
9 19624
bartonc
6,596 Recognized Expert Expert
Perhaps you are trying too hard. os.walk() will do the work for you:
walk( top[, topdown=True [, onerror=None]])

walk() generates the file names in a directory tree, by walking the tree either top down or bottom up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).
dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..'). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(di rpath, name).

If optional argument topdown is true or not specified, the triple for a directory is generated before the triples for any of its subdirectories (directories are generated top down). If topdown is false, the triple for a directory is generated after the triples for all of its subdirectories (directories are generated bottom up).

When topdown is true, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again. Modifying dirnames when topdown is false is ineffective, because in bottom-up mode the directories in dirnames are generated before dirpath itself is generated.

By default errors from the os.listdir() call are ignored. If optional argument onerror is specified, it should be a function; it will be called with one argument, an OSError instance. It can report the error to continue with the walk, or raise the exception to abort the walk. Note that the filename is available as the filename attribute of the exception object.
There are others as well.
Jul 16 '07 #2
bartonc
6,596 Recognized Expert Expert
Perhaps you are trying too hard. os.walk() will do the work for you:There are others as well.
Unless you really want the experience of writing a directory walker; in which case, I'll have a look at what you got after work.
Jul 16 '07 #3
smoothoperator12
4 New Member
hi bartonc,


Thanks a million for taking a look!!


I would definitely like to learn how to write my own directory walker, and
it is nice to know that if I learn how to use os.walk() that I can get the output
I want, however I really would like to know where my code is going haywire at,
and I am really confused why it goes down to the third level of the directory
tree and then decides to crap out.


Thanks again so much.
Jul 16 '07 #4
bvdet
2,851 Recognized Expert Moderator Specialist
Expand|Select|Wrap|Line Numbers
  1.  
  2. import os
  3. import sys
  4. import dircache
  5. import stat
  6. import string
  7.  
  8.  
  9.  
  10.  
  11. def dirrecur(currsub):
  12.  
  13.  
  14.         thispath = os.getcwd()
  15.  
  16.         print thispath
  17.         print "\n"
  18.  
  19.  
  20.         if(len(currsub) == 0):   #if len = 0, no subdirectories were found.
  21.  
  22.                 os.chdir('..')   #go back up one step.
  23.  
  24.         else:
  25.                 dlist = []
  26.  
  27.                 for z in range(0,len(currsub)):
  28.  
  29.  
  30.                         os.chdir(currsub[z])
  31.  
  32.                         print currsub[z]
  33.                         print "\n"
  34.  
  35.                         direntries = dircache.opendir('.') #open this directory
  36.  
  37.                         for j in range(0,len(direntries)):
  38.  
  39.                                 statresult = os.stat(direntries[j])
  40.  
  41.                                 if (stat.S_ISDIR(statresult.st_mode) != 0):
  42.  
  43.  
  44.                                         dlist.append(direntries[j])     #dlist is built here.
  45.                                         print direntries[j]
  46.  
  47.                                 else:
  48.  
  49.                                         print direntries[j]
  50.  
  51.                          dirrecur(dlist)   #recur again with full list of subdirectories.
  52.  
  53.  
  54.  
  55. #MAIN
  56.  
  57.  
  58. abspath = os.getcwd()
  59.  
  60. direntries = dircache.opendir(abspath)
  61.  
  62. dirlist = []
  63.  
  64.  
  65.  
  66. for i in range(0,len(direntries)):
  67.  
  68.  
  69.         statresult = os.stat(direntries[i])
  70.  
  71.         if (stat.S_ISDIR(statresult.st_mode) != 0): #if it's a directory file.
  72.  
  73.                 dirlist.append(direntries[i])
  74.                 print direntries[i]                 #print it out too.
  75.  
  76.         else:
  77.  
  78.                 print direntries[i]
  79.  
  80.  
  81.  
  82. dirrecur(dirlist)
  83.  
  84.  
  85.  
I think it would be simpler to use os.listdir(). Check out the thread HERE.

<Mod EDIT: Nice recovery below, BV. Admin is still trying to work the bugs out of code tags inside quotes. The weird thing is that it doesn't always happen. I've removed the quote here, rather than the code tags.>
Jul 16 '07 #5
bvdet
2,851 Recognized Expert Moderator Specialist
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 subdirectories.





It goes down to level 3 of the directory tree, but at one point I get the
error 2, the no such file or directory message. I have checked the
file permissions of the directory that it gets stuck on, and I have no idea why
it is gettintg stuck on that directory and not one of the directories before it.

I would be extremely grateful if someone could lend another set of eyes to my
code and see where I'm going wrong at, and why it goes to level 3 so to speak
of the tree and then fails.

I'm stumped!


Thanks so much in advance. Any and all input would be greatly appreciated!
I think it would be simpler to use os.listdir(). Check out the thread HERE.
BV
Jul 16 '07 #6
smoothoperator12
4 New Member
If it helps any what I am looking for in my output,

I want the output to resemble the UNIX program ls -R as closely as possible.


I am running solaris 2.10.


I notice that when I use os.walk(), that each directory is showing files that
even ls -a won't show on those directories.

Are those files placed there by the system administrator?
Jul 16 '07 #7
bartonc
6,596 Recognized Expert Expert
If it helps any what I am looking for in my output,

I want the output to resemble the UNIX program ls -R as closely as possible.


I am running solaris 2.10.


I notice that when I use os.walk(), that each directory is showing files that
even ls -a won't show on those directories.

Are those files placed there by the system administrator?
I'm afraid that you are dealing with a bunch of Windows users, here.
Not going to be much help on your output format, but, with regard to that, I do see a need to pass the recursion depth into the next iteration. Perhaps starting with a very simple, clean example of a recursive directory walker would help. I really like the simplicity of copytree() from the shutil module:
Expand|Select|Wrap|Line Numbers
  1.  
  2. def copytree(src, dst, symlinks=False):
  3.     """Recursively copy a directory tree using copy2().
  4.  
  5.     The destination directory must not already exist.
  6.     If exception(s) occur, an Error is raised with a list of reasons.
  7.  
  8.     If the optional symlinks flag is true, symbolic links in the
  9.     source tree result in symbolic links in the destination tree; if
  10.     it is false, the contents of the files pointed to by symbolic
  11.     links are copied.
  12.  
  13.     XXX Consider this example code rather than the ultimate tool.
  14.  
  15.     """
  16.     names = os.listdir(src)
  17.     os.mkdir(dst)
  18.     errors = []
  19.     for name in names:
  20.         srcname = os.path.join(src, name)
  21.         dstname = os.path.join(dst, name)
  22.         try:
  23.             if symlinks and os.path.islink(srcname):
  24.                 linkto = os.readlink(srcname)
  25.                 os.symlink(linkto, dstname)
  26.             elif os.path.isdir(srcname):
  27.                 copytree(srcname, dstname, symlinks)
  28.             else:
  29.                 copy2(srcname, dstname)
  30.             # XXX What about devices, sockets etc.?
  31.         except (IOError, os.error), why:
  32.             errors.append((srcname, dstname, why))
  33.         # catch the Error from the recursive copytree so that we can
  34.         # continue with other files
  35.         except Error, err:
  36.             errors.extend(err.args[0])
  37.     if errors:
  38.         raise Error, errors
So, what's that? Maybe 3 lines to get the name sorted out and 3 more to decide where or not to do the recursion.
Jul 17 '07 #8
ghostdog74
511 Recognized Expert Contributor
If it helps any what I am looking for in my output,

I want the output to resemble the UNIX program ls -R as closely as possible.


I am running solaris 2.10.


I notice that when I use os.walk(), that each directory is showing files that
even ls -a won't show on those directories.

Are those files placed there by the system administrator?
what does not show in ls -a that is shown in os.walk? here's a little walker for you
Expand|Select|Wrap|Line Numbers
  1. from os import listdir, sep
  2. from os.path import isdir
  3. def skywalker(dir):
  4.     for file in listdir(dir):
  5.         path = dir + sep + file        
  6.         if isdir(path):
  7.             print "path: ",path
  8.             skywalker(path)
  9.         else:
  10.             print '->' + file
  11. skywalker("/home/test")
  12.  
Jul 17 '07 #9
bvdet
2,851 Recognized Expert Moderator Specialist
I have been using this function:
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. def dirEntries(dir_name, subdir, *args):
  4.     '''Return a list of file names found in directory 'dir_name'
  5.     If 'subdir' is True, recursively access subdirectories under 'dir_name'.
  6.     Additional arguments, if any, are file extensions to match filenames. Matched
  7.         file names are added to the list.
  8.     If there are no additional arguments, all files found in the directory are
  9.         added to the list.
  10.     Example usage: fileList = dir_list(r'H:\TEMP', False, 'txt', 'py')
  11.         Only files with 'txt' and 'py' extensions will be added to the list.
  12.     Example usage: fileList = dir_list(r'H:\TEMP', True)
  13.         All files and all the files in subdirectories under H:\TEMP will be added
  14.         to the list.
  15.     '''
  16.     fileList = []
  17.     for file in os.listdir(dir_name):
  18.         dirfile = os.path.join(dir_name, file)
  19.         if os.path.isfile(dirfile):
  20.             if len(args) == 0:
  21.                 fileList.append(dirfile)
  22.             else:
  23.                 if os.path.splitext(dirfile)[1][1:] in args:
  24.                     fileList.append(dirfile)
  25.         # recursively access file names in subdirectories
  26.         elif os.path.isdir(dirfile) and subdir:
  27.             print "Accessing directory:", dirfile
  28.             fileList += dirEntries(dirfile, subdir, *args)
  29.     return fileList
Jul 17 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3148
by: Ruby Tuesday | last post by:
Is there a fast way to read files/directory recursively? Instead of inspecting each file(s)/dir(s), is there a way to know that its a file or a directory from its hidden attribut both for windows or unix filesystem? Thanks
9
4967
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
1
3360
by: Antonio Lopez Arredondo | last post by:
hi all !!! I need to copy a folder and its subfolders to another location; which class should I use ? could only find the System.IO.Directory.MOVE but don't know how to COPY. thanks in advance, ant
1
2334
by: Fatemeh | last post by:
Hi I want to write a program (FileCopier) that it allow the user to check files (or entire directories) in the left tree view (source). If the user presses the Copy button, the files checked on the left side will be copied to the Target Directory specified in the right-hand control. If the user presses Delete, the checked files will be deleted. And the user interface for FileCopier consists of the following controls:
3
4697
by: dibblm | last post by:
Below is current code used. I can only list one directory then move to next. I want to search one more directory further and can't seem to find how to get one deeper. What I want to accomplish is to get to a specified directory, Then list all the files of that directory. Move to the next and do the same. J:\ -user1 --Cookies ---List all files
3
1722
by: Chris | last post by:
I am clearly missing something simple here.... I have a function (fileTree) that recursively reads all the files in a directory passed to it, and returns an array of the files, I have included the code for this function below. When I call this function, the bit of debug code at the end will correctly print the array of files I am expecting, and then returns. However, what is returned form the function isnt the array, is a scalar that
1
9917
by: Aek | last post by:
What is the best way to recursively change the permissions of the directory we are installing into? Is there a nice way to do this in C# ..NET? We are using an MSI installer and will need to add some custom actions to change the permissions on the install directory and its sub folders/files so that they can be accessed correctly via a network share later on. We would want to add Everyone full access to the install directory (its
3
1885
by: empiresolutions | last post by:
I'm trying to build my first PHP Class. After days of tweaking, im lost. I am used to working with functions and arrays, but wrapping them in classes is confusing me. The following code is to return an Array Collection representative of a folder and sub folder in a directory of choice. It doesn't work. Throws an error "Call to undefined function: parse_dir()". It would be great if someone out there could see where im not correct. I've...
2
2407
by: sebastien.abeille | last post by:
Hello, I would like to create a minimalist file browser using pyGTK. Having read lot of tutorials, it seems to me that that in my case, the best solution is to have a gtk.TreeStore containing all the files and folders so that it would map the file system hierarchy.
0
9594
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
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
10351
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,...
0
10096
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9174
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7638
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
6866
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3002
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.