Python
  Home arrow Python arrow Page 4 - 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 
IBM Developerworks
 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    File Management in Python - Pickling Data
    (Page 4 of 5 )

     

    Using the methods covered in the previous section, it is possible to read strings from files and write strings to files. However, in some situations, you may need to pass other types of data, such as lists, tuples, dictionaries and other objects. In Python, this is possible through a method known as pickling. To pickle data, you would use the "pickle" module included in the standard library.

     

    Let's start by pickling a short list of strings and integers: 

    import pickle

    fileHandle = open ( 'pickleFile.txt', 'w' )

    testList = [ 'This', 2, 'is', 1, 'a', 0, 'test.' ]

    pickle.dump ( testList, fileHandle )

    fileHandle.close() 

    Unpickling the data is just as easy: 

    import pickle

    fileHandle = open ( 'pickleFile.txt' )

    testList = pickle.load ( fileHandle )

    fileHandle.cloes() 

    We can also store more complex data: 

    import pickle

    fileHandle = open ( 'pickleFile.txt', 'w' )

    testList = [ 123, { 'Calories' : 190 }, 'Mr. Anderson', [ 1, 2, 7 ] ]

    pickle.dump ( testList, fileHandle )

    fileHandle.close()

     

    import pickle

    fileHandle = open ( 'pickleFile.txt' )

    testList = pickle.load ( fileHandle )

    fileHandle.close() 

    As you can see, pickling is extremely easy to do with Python's "pickle" module. Numeous objects may be stored in files with it. You can also use the "cPickle" module if it is availible to you. It's exactly the same as the "pickle" modue, but it's faster: 

    import cPickle

    fileHandle = open ( 'pickleFile.txt', 'w' )

    cPickle.dump ( 1776, fileHandle )

    fileHandle.close() 

    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...




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