Searching for Files Based on Extension : path « File « Python Tutorial

Python Tutorial
1. Introduction
2. Data Type
3. Statement
4. Operator
5. String
6. Tuple
7. List
8. Dictionary
9. Collections
10. Function
11. Class
12. File
13. Buildin Function
14. Buildin Module
15. Database
16. Regular Expressions
17. Thread
18. Tkinker
19. wxPython
20. XML
21. Network
22. CGI Web
23. Windows
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorial
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Python Tutorial » File » path 
12. 21. 6. Searching for Files Based on Extension
import os
path = "/books/python"
pattern = "*.py;*.doc"

def printFiles(dirList, spaceCount, typeList):
    for file in dirList:
        for ext in typeList:
            if file.endswith(ext):
                print "/".rjust(spaceCount+1+ file
                break

def printDirectory(dirEntry, typeList):
    print dirEntry[0"/"
    printFiles(dirEntry[2], len(dirEntry[0]),
typeList)

extList = []
for ext in pattern.split(";"):
    extList.append(ext.lstrip("*"))


for directory in os.walk(path):
    printDirectory(directory, extList)
12. 21. path
12. 21. 1. Module Attributes to Aid in Multi-platform Development
12. 21. 2. os.path.abspath(path) returns a string version of the absolute path of the path specified
12. 21. 3. os.path module provides exists(path), isdir(path), and isfile(path) to check for the existence of files and directories
12. 21. 4. os.chdir(path) changes the current working directory
12. 21. 5. Walking the Directory Tree
12. 21. 6. Searching for Files Based on Extension
12. 21. 7. Working with Directories
12. 21. 8. Splitting Pathnames
12. 21. 9. Listing Directories
12. 21. 10. Listing Directories with glob
12. 21. 11. Get full path
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.