Python
  Home arrow Python arrow Page 2 - File Management in Python
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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: 5 stars5 stars5 stars5 stars5 stars / 74
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    PCmover - $15 Off with Coupon Code CJPH7Q

    File Management in Python - Getting Information on Existing Files
    (Page 2 of 5 )

     

    Using several of Python's modules, it is possible to obtain information on existig files. To get basic information, the "os" module can be used in conjunction with the "stat" module: 

    import os

    import stat

    import time

    fileStats = os.stat ( 'test.txt' )

    fileInfo = {

       'Size' : fileStats [ stat.ST_SIZE ],

       'LastModified' : time.ctime ( fileStats [ stat.ST_MTIME ] ),

       'LastAccessed' : time.ctime ( fileStats [ stat.ST_ATIME ] ),

       'CreationTime' : time.ctime ( fileStats [ stat.ST_CTIME ] ),

       'Mode' : fileStats [ stat.ST_MODE ]

    }

    for infoField, infoValue in fileInfo:

       print infoField, ':' + infoValue

    if stat.S_ISDIR ( fileStats [ stat.ST_MODE ] ):

       print 'Directory. '

    else:

       print 'Non-directory.' 

    The above example creates a dictionary containing some basic information about the file. It then displays the data and tells us if it's a directory or not. We can also check to see if the file is one of several other types: 

    import os

    import stat

    fileStats = os.stat ( 'test.txt' )

    fileMode = fileStats [ stat.ST_MODE ]

    if stat.S_ISREG ( fileStats [ stat.ST_MODE ] ):

       print 'Regular file.'

    elif stat.S_ISDIR ( fileSTats [ stat.ST_MODe ] ):

       print 'Directory.'

    elif stat.S_ISLNK ( fileSTats [ stat.ST_MODe ] ):

       print 'Shortcut.'

    elif stat.S_ISSOCK ( fileSTats [ stat.ST_MODe ] ):

       print 'Socket.'

    elif stat.S_ISFIFO ( fileSTats [ stat.ST_MODe ] ):

       print 'Named pipe.'

    elif stat.S_ISBLK ( fileSTats [ stat.ST_MODe ] ):

       print 'Block special device.'

    elif stat.S_ISCHR ( fileSTats [ stat.ST_MODe ] ):

       print 'Character special device.' 

    Additionally, we can use "os.path" to gather basic information: 

    import os.path

    fileStats = 'test.txt'

    if os.path.isdir ( fileStats ):

       print 'Directory.'

    elif os.path.isfile ( fileStats ):

       print 'File.'

    elif os.path.islink ( fileStats ):

       print 'Shortcut.'

    elif os.path.ismount ( fileStats ):

       print 'Mount point.' 

    More Python Articles
    More By Peyton McCullough


       · Hello!I wrote this article with a "less talk and more code" approach. Does...
       · Good article, I like the code-heavy approach. Another module I find useful is...
       · I like the more-code approach. A nice article.When you're starting out, it's...
       · fileHandle = open ( 'usernames.txt','ab' )fileHandle.write (raw_input("Please...
       · fileHandle.write ( raw_input ( 'Please enter user name to add: ' ) + '\n' )...
       · Hey. I am a python newbie, and reading the "File Management" article helped me...
       · following the nice file i/o tutorial here, i have only one remaining quesiton: how...
       · The article may be good, but it does not explain how to avoid program exit when file...
       · Be consistent with your backslashes, this will solve your problem!
       · thats a more general issue of exception handling, IMHO
       · I get errors when I try to run the first example in - Getting Information on...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway