Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Python 101 (part 5): Snake Oil For The Soul
Buy some Air In A Bottle(tm)! You won't regret it!

| Air In A Bottle |

Like all widely-used programming languages, Python has the very useful ability to read data from, and write data to, files on your system. It accomplishes this via "file handles" - a programming construct that allows Python scripts to communicate with data structures like files, directories and other Python scripts.

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

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
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 the file and assign it a file handle - this file handle can then be used to interact 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 the read() function to read everything in the file, and the close() function to close the handle after I'm done.

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


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback