Python
  Home arrow Python arrow Page 5 - 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

    Dell PowerEdge Servers

    File Management in Python - Creating In-memory Files
    (Page 5 of 5 )

     

    A number of modules you will encounter contain methods that require a file object as an argument. Sometimes, it is inconvenient to create and use a real file, however. Thankfully, you can create files that store themselves in a computer's memory using the "StringIO" module: 

    import StringIO

    fileHandle = StringIO.StringIO ( "Let freedom ring." )

    print fileHandle.read() # "Let freedom ring."

    fileHandle.close() 

    A "cStringIO" module is also availible. It is identical to the "StringIO" module in use, but, just like the "cPickle" module is to the "pickle" module, it is faster: 

    import cStringIO

    fileHandle = cStringIO.cStringIO ( "To Kill a Mockingbird" )

    print fileHandle.read() # "To Kill a Mockingbid"

    fileHandle.close() 

    Conclusion

     

    File management is a task that programmers of many languages will often encounter in their applications. Thankfully, Python makes the task incredibly easy compared to other languages. It offers many modules in its standard library that aid programmers, and its object orientation further simplifies things.

     

    You now have a basic understanding of file management in Python, and you will use it in many future applications.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 3 hosted by Hostway