Python
  Home arrow Python arrow Page 3 - File Management in Python
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

File Management in Python
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 89
    2005-01-31


    Table of Contents:
  • File Management in Python
  • Getting Information on Existing Files
  • Directories
  • Pickling Data
  • Creating In-memory Files

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    File Management in Python - Directories
    ( Page 3 of 5 )

     

    Directories, like regular files, are easy to work with. Let's start by listing the contents of a directory: 

    import os

    for fileName in os.listdir ( '/' ):

       print fileName 

    As you can see, this is extremely simple, and it can be done in three lines.

     

    Creating a directory is also simple: 

    import os

    os.mkdir ( 'testDirectory' ) 

    It is equally as easy to delete the directory we just created: 

    import os

    os.rmdir ( 'testDirectory ) 

    We can also create multiple directories at a time: 

    import os

    os.makedirs ( 'I/will/show/you/how/deep/the/rabbit/hole/goes' ) 

    Assuming we add nothing to the directories we just created, we can also delete them all at once: 

    import os

    os.removedirs ( 'I/will/show/you/how/deep/the/rabbit/hole/goes' ) 

    Suppose we want to perform a specific action when a specific file type is reached. This can easily be done with the "fnmatch" module. Let's print the contents of all the ".txt" files we encounter and print the filename of any ".exe" files we encounter: 

    import fnmatch

    import os

    for fileName in os.listdir ( '/' ):

       if fnmatch.fnmath ( fileName, '*.txt' ):

          print open ( fileName ).read()

       elif fnmatch.fnmatch ( fileName, '*.exe' ):

          print fileName 

    The asterisk character can represent any amount of characters. If we want to match just one character, we can use the question mark: 

    import fnmatch

    import os

    for fileName in os.listdir ( '/' ):

       if fnmatch.fnmatch ( fileName, '?.txt' ):

          print 'Text file.' 

    It is also possible to create a regular expression using the "fnmatch" module, matching filenames with the "re" module:

     

    import fnmatch

    import os

    import re

    filePattern = fnmatch.translate ( '*.txt' )

    for fileName in os.listdir ( '/' ):

       if re.match ( filePattern, fileName ):

          print 'Text file.'

     

    If we're just looking for one type of filename, it is a lot easier to use the "glob" module. Its patterns are similar to those used in "fnmatch": 

    import glob

    for fileName in glob.glob ( '*.txt' ):

       print 'Text file.' 

    It is also possible to use ranges of characters in the patterns, just as you would in regular expressions. Suppose you want to print the names of text files with one digit before the extension: 

    import glob

    for fileName in glob.glob ( '[0-9].txt' ):

       print fileName 

    The "glob" module makes use of the "fnmatch" module.

     



     
     
    >>> More Python Articles          >>> More By Peyton McCullough
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT