How to list files, folders and all their files under the folders 
Author Message
 How to list files, folders and all their files under the folders

Hi,
Is there a way to list all files, folders and all the files under those
folders using python.

Thanks.



Sat, 08 Nov 2003 05:55:10 GMT  
 How to list files, folders and all their files under the folders

Quote:

> Hi,
> Is there a way to list all files, folders and all the files under those
> folders using python.

> Thanks.

You need to see the docs for os.path.walk()
http://www.python.org/doc/current/lib/module-os.path.html


Sat, 08 Nov 2003 06:29:44 GMT  
 How to list files, folders and all their files under the folders
Using os.path.walk you can traverse a directory tree and return all
directories and files to a list ... the below example takes it a step
further and generates fullpath names in the list DirectoryList

import os

DirectoryToWalk='c:\\Test\\'    #Specify Directory To Traverse

def walker(arg, dirname, filenames):
    # Create A List Of Files with full pathname
    for files in filenames:
        arg.append(os.path.join(dirname, files))

DirectoryList = []                      #Write Full Pathnames Here

os.path.walk(DirectoryToWalk, walker, DirectoryList)

Hope this helps

Ben

On Mon, 21 May 2001 21:55:10 GMT, "Jennifer Jeng"

Quote:

>Hi,
>Is there a way to list all files, folders and all the files under those
>folders using python.

>Thanks.



Sat, 08 Nov 2003 07:53:43 GMT  
 How to list files, folders and all their files under the folders

Quote:
> Hi,
> Is there a way to list all files, folders and all the files under those
> folders using python.

os.path.walk() is one way -- there are others, but I think that
one is easiest.  For example:

D:\Python21>python
python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.

Quote:
>>> import os.path
>>> def show(arg, dirname, names):

...     print dirname,names
...
Quote:
>>> os.path.walk('Lib/xml',show,None)

Lib/xml ['CVS', 'dom', 'parsers', 'sax', '__init__.py']
Lib/xml\CVS []
Lib/xml\dom ['CVS', 'domreg.py', 'minidom.py', 'pulldom.py', '__init__.py']
Lib/xml\dom\CVS []
Lib/xml\parsers ['CVS', 'expat.py', '__init__.py']
Lib/xml\parsers\CVS []
Lib/xml\sax ['CVS', 'expatreader.py', 'handler.py', 'saxutils.py',
'xmlreader.py
', '_exceptions.py', '__init__.py']
Lib/xml\sax\CVS []

Alex



Sat, 08 Nov 2003 16:14:51 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. using list box to find specific files in folderS

2. putting all files from a folder into a list

3. File Listing for All Folders

4. Hierarchial ListBoxes -- expanded folder inside a folder?

5. creating folders and sub folders

6. Password protect, lock, hide and secure files, folders and drives on your computer 5741

7. Invisible files and folders

8. Password protect, lock, hide and secure files, folders and drives on your computer 1335

9. Select File or Folder

10. Putting files in a folder...

11. Dropping folder of files onto RB application

12. Cretaing a file in a scpecific folders

 

 
Powered by phpBB® Forum Software