Python
  Home arrow Python arrow Page 5 - Python 101 (part 5): Snake Oil For The...
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
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

Python 101 (part 5): Snake Oil For The Soul
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2001-07-09

    Table of Contents:
  • Python 101 (part 5): Snake Oil For The Soul
  • Air In A Bottle
  • Treading The Right Path
  • Standing In Line
  • Learning To Write
  • Desperately Seeking Python
  • String Theory
  • Pop() Goes The Weasel

  • 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

    Python 101 (part 5): Snake Oil For The Soul - Learning To Write


    (Page 5 of 8 )

    Obviously, reading a file is no great shakes - but how about writing to afile?

    Well, it isn't all that hard either - you just need to open the file withan optional argument indicating that you would like to write to it.

    >>> data = open("snakeoil.txt", "w") >>>
    The optional argument specifies the mode in which the file should beopened, and may be either one of "r" (read only), "w" (write) or "a"(append). Opening a file with "w" is akin to creating a new, empty file -the previous contents of the file are erased. Opening a file with "a"retains the previous contents of the file and appends new material to theend of the file.

    You can add the "+" operator to the three modes above to indicate that bothread and write operations should be allowed.

    Just as Python has the read() and readlines() methods for file read, it hascorresponding write() and writelines() functions for file output. Thewrite() function writes a single string to the file, as demonstrated in thefollowing example:

    >>> data = open("snakeoil.txt", "a") >>> data.write("-- end of commercial --") >>> data.close() >>>


    $ cat snakeoil.txt After years of research, scientists at AB Labs have come up with a radical new product, once that promises to improve the quality of life for humans and animals everywhere. We call it Air In A Bottle(tm), and we firmly consider it to be one of our best inventions ever. Carry it around with you (the bottle is titanium, the same material used in rocket ships, and the air is, well, air) and inhale from it whenever you need some air. Or give it to your friends as a present (after all, they need air too!) Air In A Bottle(tm). Get some today! -- end of commercial --$
    It should be noted that write() does not automatically add a line break toyour string; this needs to be done manually.

    The writelines() method takes a list and writes each element of the list tothe file sequentially. The following Python program builds a list from userinput, and then writes the list to the file.

    #!/usr/bin/python # empty list comments = [] # ask for three comments and build list for x in range (0,3): rawInput = raw_input("Enter a comment: ") comments.append("User comment:" + rawInput + "\n") # open file data = open("snakeoil.txt", "a") # write list data.writelines(comments) # clean up data.close()
    Here's what it looks like:

    Enter a comment: This product sucks! Enter a comment: How stupid do you think we are?????? Enter a comment: Can I have some more? $ cat snakeoil.txt After years of research, scientists at AB Labs have come up with a radical new product, once that promises to improve the quality of life for humans and animals everywhere. We call it Air In A Bottle(tm), and we firmly consider it to be one of our best inventions ever. Carry it around with you (the bottle is titanium, the same material used in rocket ships, and the air is, well, air) and inhale from it whenever you need some air. Or give it to your friends as a present (after all, they need air too!) Air In A Bottle(tm). Get some today! User comment:This product sucks! User comment:How stupid do you think we are?????? User comment:Can I have some more?
    As you can see, the comments entered at the command line have been appendedto the end of the file.

    More Python Articles
    More By Vikram Vaswani, (c) Melonfire


     

       

    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 1 hosted by Hostway