June 2002
Beginner to intermediate
640 pages
14h 24m
English
Before we begin our tour of files, we'll create a directory, c:\dat, where we'll put the data file we'll be working with throughout the chapter.
In Python, file I/O is built into the language, so working with files doesn't require an API or library. You just make a call to the intrinsic (built-in) open() function, which returns a file object. The typical form of open() is open(filename, mode).
Say, for example, that you want to open a file in c:\dat and write "Hello World" to it. Here's how to do that. (Start up the interactive interpreter and follow along.)
>>> file = open("c:\\dat\\hello.txt", "w")
>>> file.write("Hello World ")
>>> file.write("Hello Mars ")
>>> file.write("Hello Jupiter ")
>>> file.close()
Now, using ...
Read now
Unlock full access