Home arrow Python arrow Page 2 - Python 101 (part 5): Snake Oil For The Soul

Air In A Bottle - Python

Now that you know how to create and use Python's tuples, listsand dictionaries, it's time to get your hands dirty. In this article, findout how to read from, and write to, files on the filesystem with built-inPython methods, and also take a look at some of the new features availablein Python 2.x.

TABLE OF CONTENTS:
  1. Python 101 (part 5): Snake Oil For The Soul
  2. Air In A Bottle
  3. Treading The Right Path
  4. Standing In Line
  5. Learning To Write
  6. Desperately Seeking Python
  7. String Theory
  8. Pop() Goes The Weasel
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 4
July 09, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Like all widely-used programming languages, Python has the very usefulability to read data from, and write data to, files on your system. Itaccomplishes this via "file handles" - a programming construct that allowsPython scripts to communicate with data structures like files, directoriesand other Python scripts.

For my first example, I'll assume that I have a text file called"snakeoil.txt", containing the following commercial:

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!
Now, in order to read this data into a Python program, I need to open thefile and assign it a file handle - this file handle can then be used tointeract with the data.

Python 1.5.2 (#1, Aug 25 2000, 09:33:37) [GCC 2.96 20000731 (experimental)] on linux-i386 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> data = open("snakeoil.txt") >>> data.read() '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.\012 \012We call it Air In A Bottle(tm), and we firmly consider it to be one of our best inventions ever.\012\012Carry 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!)\012\012Air In A Bottle(tm). Get some today!\012' >>> data.close() >>>
In this case, I've first used the open() function to open the file"snakeoil.txt" and assign it to the file handle "data". Next, I've used theread() function to read everything in the file, and the close() function toclose the handle after I'm done.

Since this is the command-line interpreter, a read() call will display thecontents of the file on the console, complete with line breaks (in caseyou're wondering, that's what \012 is.) The read() function reads theentire file into a single string.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- 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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: