1.1 打开文件进行读取操作


In [2]: file = open ("record.txt","r")

In [3]: print file.read()
The system information is:

说明:

open是内建函数,不用import载入;

open具有3个参数:

       文件名   必须写

      打开模式     r , w ,a , 一个补充模式 b , 表示二进制模式

      缓冲区大小    表示缓冲文件的操作方式

In [14]: file = open ("record.txt","w")

In [15]: file.write("this is a test of write \n")

In [16]: file.write("this is a test of write \n")                 

In [17]: file.close()

root@test-desktop:/home/lijy# cat record.txt
this is a test of write
this is a test of write                                     查看写了两次内容

方法二:

In [20]: try:
    file = open("record.txt","w")                              file前有缩进
    file.write("this is another method of write\n")                 file前有缩进
finally:
    file.close()
   ....:     
   ....:     

In [25]:
root@test-desktop:/home/lijy# cat record.txt
this is another method of write

以上使用了try/finally将文件进行封闭调用,不论执行中是否有异常发生,finally都会被执行;

方法 三:

In [25]: from __future__ import with_statement

In [26]: with open("record.txt","w") as file:
   ....:     file.write("this is the third way to write\n")
   ....:     
   ....:     

root@test-desktop:/home/lijy# cat record.txt
this is the third way to write
使用上下文管理器,with语句,优点为退出时自动关闭文件

1.2 读取文件

In [29]: f = open("record.txt","r")

In [30]: f.read()                           默认读到结尾;
Out[30]: 'this is the third way to write\n'


In [32]: f = open("record.txt","r")

In [33]: f.read(10)                        读取指定长度
Out[33]: 'this is th'


In [34]: f = open("record.txt","r")

In [35]: f.readline()                                  读取一行          
Out[35]: 'this is the third way to write\n'

In [36]: f.readline(3)                           继续读取指定字节
Out[36]: 'tes'

In [37]: f.readlines()                             读取到结尾
Out[37]: ['t test\n', 'test\n', 'test enough\n', 'end\n']

In [38]: f.readlines(7)                      已读取到结尾,不能再进行
Out[38]: []

1.3 写文件

方法一:用write,见1.1

方法二:writelines()            必须有一个参数(序列),可以为任何迭代对象类型:列表,元组,组合列表,发生器;

In [39]: f = open("record.txt","w")

In [40]: f.writelines("%s\n" % i for i in range (10))

In [41]: f.close()

In [42]: g = open("record.txt","r")

In [43]: g.read                少了括号
Out[43]: <built-in method read of file object at 0x9cf65a0>

In [44]: g.read()
Out[44]: '0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n'

1.4 标准输入(将数据送入程序,使程序运行时读取)、输出(程序执行时执行写入操作)

In [45]: import sys

In [46]: f = open("record.txt","r")

In [47]: sys.stdin
Out[47]: <open file '<stdin>', mode 'r' at 0xb77b1020>

In [48]: f
Out[48]: <open file 'record.txt', mode 'r' at 0x9d7ad30>

In [49]: type(sys.stdin)== type(f)
Out[49]: True

先打开磁盘上的文件来创建一个文件对象,再用sys.stdin引用访问,python解释器视open和sys.stdin为相同类型;

同理,可读文件open与可写文件sys.stdout也具有相同的类型,不再进行举例;

 #!/usr/bin/python
  2 #filename: aaa.py
  3
  4 import sys
  5
  6 for i, line in enumerate(sys.stdin):                     加载sys模块并使用stdin属性时,可以使用标准输入,同理也可以stdout进行输出
  7     print "%s: %s" % (i, line)

root@test-desktop:/home/lijy# who | python aaa.py
0: test     tty7         2012-06-15 11:42 (:0)

1: test     pts/0        2012-06-25 08:48 (172.16.7.94)

2: test     pts/1        2012-06-25 08:48 (172.16.7.94)

3: test     pts/2        2012-06-25 08:48 (172.16.7.94)


















posted on 2022-07-05 18:14  我在全球村  阅读(29)  评论(0编辑  收藏  举报