1. Home
  2. Computing & Technology
  3. Python

Top 4 Ways to Work With Files in Python

by Al Lukaszewski
for About.com

When computers are not reading code, they are processing data. Data is usually stored in a file. These tips show you how to do some common tasks related to files using Python.

1. Analyzing a File Line-By-Line With Python

Particularly when programming for the World Wide Web, it is frequently necessary to limit how much input data is read at a time. In this tip, I show how to read data from a file repeatedly, gradually reading and processing all of it.

2. Processing a File All-At-Once With Python

When one has plenty of programming resources to spare, it is often easier to read all of the data from a file into a dictionary. This article shows you how.

3. Moving Around in a File With Python

In my experience, one seldom has the leisure of reading data from discrete sources. Very often, one needs to read data from multiple points in a file and evaluate the results. This tip shows how to do that.

4. Reading a Single Line from a File With Python

Depending on the architecture of your program, you may need to read the same line from a file more than once or every time the program executes. If so, you should acquaint yourself with the module linecache. Here I show you how to read a single line from the middle of a file, leaving the other lines untouched.
Explore Python
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Python
  4. ScripTips
  5. Ways to Work With Files in Python>

©2009 About.com, a part of The New York Times Company.

All rights reserved.