Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Opening Files and File Objects - Page 9

May 24, 2001

Probably the single most common thing you will want to do with files is open and read them. In Python, opening and reading a file is accomplished using the built-in open function, and various different built-in reading operations. This following short Python program reads in one line from a text file named "myfile":

fileobject = open("myfile", 'r')
line = fileobject.readline()

open doesn't actually read anything from the file; instead it returns an abstract object called a file object, representing the opened file. All Python file I/O is done using file objects, rather than file names.

Use open to open a file for reading or writing. open returns a file object.

The first call to readline returns the first line in the file object, everything up to and including the first newline character, or all of the file if there is no newline character in the file; the next call to readline would return the second line, and so on. So, a file object is something that keeps track of a file and how much of the file has been read, or written. The first argument to the open function is a pathname. In the above example we are opening what we expect to be an already existing file in the current working directory. The following would open a file at the given absolute location:

import os
fileName = os.path. join("c:", "My Documents", "test", "myfile")
fileobject = open(fileName, 'r')

Closing Files


Use close to close a file object once you are done with it.


Once all data has been read from or written to a file object, it should be closed. Closing a file object frees up system resources, allows the underlying file to be read or written to by other code, and, in general, makes the program more reliable. In small scripts, not closing a file object will generally not have much of an effect; file objects are automatically closed when the script or program terminates. In larger programs, too many open file objects may exhaust system resources, causing the program to abort.

Closing file objects is done using the close method, after the file object is no longer needed. The short program above then becomes this:

fileobject = open("myfile", 'r')
line = fileobject.readline()

# . . . any further reading on the fileobject . . . fileobject.close()

Opening Files in Write or Other Modes

The second argument of the open command is a single character denoting how the file should be opened. 'r' means open the file for reading: 'w' means open the file for writ-ing (any data already in the file will be erased): and 'a' means open the file for writing (new data will be appended to the end of any data already in the file). If you want to open the file for reading, you can leave out the second argument. 'r' is the default. The follow-ing short program writes "Hello, World" to a file:

fileobject = open("myfile", 'w')
fileobject.write("Hello, World\n")
fileobject.close()

Depending on the operating system, open may also have access to additional file modes. In general, these are not necessary for most purposes. As you write more advanced Python programs, you may wish to consult the Python reference manuals for details.

open will open a file for reading, (over) writing, or appending, depending on the value if its second argument, which can be 'r', 'w', or 'a'.

As well, open can take an optional third argument, which defines how reads or writes for that file are buffered. Buffering is the process of holding data in memory until enough has been requested or written to justify the time cost of doing a disk access. Again, this is not something you typically need to worry about, but as you become more advanced in your use of Python, you may wish to read up on it.

The Built-In Namespace - Page 8
The Quick Python Book
Functions to Read and Write Text or Binary Data - Page 10


Up to => Home / Authoring / Languages / Python / Quick




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers