Introduction
The game you played yesterday uses files to store game saves. The order you placed yesterday was saved in a file. That report you typed up this morning was, obviously, stored in a file as well.
File management is an important part of many applications written in nearly every language. Python is no exception to this. In this article, we will explore the task of manipulating files using several modules. We'll read, write to, append and do other strange things to files. Let's get started.
Reading and Writing
The most basic tasks involved in file manipulation are reading data from files and writing data to files. This is a very simple task to learn. Let's open a file for writing:
fileHandle = open ( 'test.txt', 'w' )
The "w" indicates that we will be writing to the file, and the rest is pretty simple to understand. The next step is to write data to the file:
fileHandle.write ( 'This is a test.\nReally, it is.' )
This will write the string "This is a test." to the file's first line and "Really, it is." to the file's second line. Finally, we need to clean up after ourselves and close the file:
fileHandle.close()
domingo, 22 de febrero de 2009
otra mas
Publicado por
DMouse
en
0:50
0
comentarios
sábado, 21 de febrero de 2009
viernes, 21 de diciembre de 2007
xD
akahsdkashdkjashdkadshkasjdakahsdkashdkjashdkadshkas
jdakahsdkashdkjashdkadshkasjdakahsdkashdkjashdkadshkasjdakahsdkashdkjashdkadshkasjdakahsdkashdkjashd
kadshkasjdakahsdkashdkjashdkadshkasjdakahsdkashdkjashdkadshkasjdakahsdkashdkjashdkadshkasjdakahsdka
shdkjashdkadshkasjdakahsdkashdkjashdkadshkasjdakahsdkashdkjashdkadshkasjdakahsdkashdkjashdkadshkasj
dakahsdkashdkjashdkadshkasjdakahsdkashdkjashdkadshkasjdakahsdkashdkjashdkadshkasjdakahsdkashdkjashdkadshkasjdakahsdkashdkjashdkadshkasjdakahsdkashdkjashdkadshkasjd
Publicado por
DMouse
en
19:24
0
comentarios
Suscribirse a:
Comentarios (Atom)