diff --git a/files.html b/files.html index fe4c577..bcad2fa 100644 --- a/files.html +++ b/files.html @@ -259,37 +259,36 @@ ValueError: I/O operation on closed file.
FIXME +
You can write to files in much the same way that you read from them. First you open a file and get a file object, then you use methods on the file object to write data to the file, then you close the file. + +
To open a file for writing, use the open() method and specify the write mode. There are two file modes for writing:
-
+
test.log (or overwriting the existing file), and opening the file for writing. The mode='w' parameter means open the file for writing. Yes, that’s all as dangerous as it sounds. I hope you didn’t care about the previous contents of that file (if any), because that data is gone now.
+write method of the file object returned by the open() function. After the with block ends, Python automatically closes the file.
+mode='a' to append to the file instead of overwriting it. Appending will never harm the existing contents of the file.
+test.log. Also note that carriage returns are not included. Since you didn’t write them explicitly to the file either time, the file doesn’t include them. You can write a carriage return with the '\n' character. Since you didn’t do this, everything you wrote to the file ended up on one line.
+
read method, the function can handle any k