Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12765
  • 博文数量: 13
  • 博客积分: 475
  • 博客等级: 下士
  • 技术积分: 125
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-09 11:20
文章分类

全部博文(13)

文章存档

2012年(4)

2010年(9)

我的朋友

分类:

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 code, and after that, I will explain it line by line.
The first program, will create a file, and put some text into it.

f=file('test.txt', 'w')
f.write('Hello world!\n')
f.close()

Only that? Yes! This program will create the file test.txt in the current working directory, and will put “Hello world!\n” into it.
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.

f=open('/home/test/Test/tally.out',  'r')
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  

The code until now is the old method. In python 2.2, the file object become enumerator itself, you'll never need to call read*() function to enumerate the lines of the file. This method is friendlier to programmers, and friendlier to the memory. It does not read all lines at once, the enumerator returns a line when it's requested using the yield return. I'll cover this feature in later articles.

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  


Attribute of file object
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.

阅读(526) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:Accessibility of Derived-to-Base Conversion

给主人留下些什么吧!~~