分类:
2010-07-29 11:21:54
Introduction
This tutorial will start with the very basis of File I/O in Python.
First Program
I will first write the co
The first program, will create a file, and put some text into it.
f.write('Hello world!\n')
f.close()
On
Here is what every line means:
f=file('test.txt', 'w'): open a file named test.txt in the cwd for writing, the file will be created if it does not exist, it will be truncted when opened for writing. I'd like to mention that file is not a function, but a class, so the f is a reference to a file object without name. The 'w' means open for writing, 'r' means reading, 'a' means appending, add a 'b' for binary file, add a '+' to allow simultaneous reading and writing.
f.write('Hello world!\n'): we call the write function of the file object, this will write the 'Hello world!\n' into the specified file.
f.close(): this call will flush the internal I/O buffer and will set the closed attribute of the file object to True. A closed file object cannot be used for further I/O operations.
That’s it! It's that easy to write in a file.
Reading A File
You saw how to write into a file. Now, we will read a file, and display it on the screen.
First, I’d like to mention, that there are several ways to read a file. I will tell you as much as I can.
lines=f.readlines()
for line in lines:
print line,
f.close()
f=open('/home/test/Test/tally.out', 'r'): You can open a file using the file class, but using open function is the prefered way. This is the prototype of this function in pydoc: open(name[, mode[, buffering]]) -> file object. You see, it will return a file object, just as newing a file class object.
lines=f.readlines(): The readlines call read all the content of the file, from begin to the end, it return a list of strings, each a line from the file.
for line in lines:
print line,
These two lines enumerates the list, and print each line. The ',' prevents the print function from adding a '\n' to the line to be printed.
The result:
10.00 20.00 30.30 40.50
20.00 30.00 45.70 66.10
40.00 75.00 107.20 55.60
50.00 20.00 30.30 40.50
70.00 1134.70 50.00 70.00
80.00 75.00 107.20 55.60
90.00 176.00 30.30 40.50
100.00 1027.45 45.70 66.10
120.00 75.00 107.20 55.60
580.00 2633.15 553.90
Grand Total 4257.55
Besides readlines function, we have read function, and readline function.
read([size]) -> read at most size bytes, returned as a string.
eg.
f.seek(0)
str=f.read(4)
print str
The result:
10.
readline([size]) -> next line from the file, as a string.
eg.
f.seek(0)
str=f.readline()
while str != '': #the readline function return an empty string at EOF
print str,
str=f.readline()
The result:
10.00 20.00 30.30 40.50
20.00 30.00 45.70 66.10
40.00 75.00 107.20 55.60
50.00 20.00 30.30 40.50
70.00 1134.70 50.00 70.00
80.00 75.00 107.20 55.60
90.00 176.00 30.30 40.50
100.00 1027.45 45.70 66.10
120.00 75.00 107.20 55.60
580.00 2633.15 553.90
Grand Total 4257.55
eg.
f.seek(0)
for line in f:
print line,
The result:
10.00 20.00 30.30 40.50
20.00 30.00 45.70 66.10
40.00 75.00 107.20 55.60
50.00 20.00 30.30 40.50
70.00 1134.70 50.00 70.00
80.00 75.00 107.20 55.60
90.00 176.00 30.30 40.50
100.00 1027.45 45.70 66.10
120.00 75.00 107.20 55.60
580.00 2633.15 553.90
Grand Total 4257.55
Name | Description |
---|---|
closed | True if the file is closed |
encoding | Open file to write |
errors | Unicode error handler |
mode | file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added) |
name | file name |
newlines |
end-of-line convention used in this file |
softspace | flag indicating that a space needs to be printed; used by print |
Ah, that's all I know about python's file i/o. Hope it's useful to you.