python-文件读写

在python中可以使用file模块和opne可以打开文件,注意python3中已经不支持file!

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
    ========= ===============================================================
    Character Meaning
    --------- ---------------------------------------------------------------
    'r'       open for reading (default)  #只读模式打开
    'w'       open for writing, truncating the file first   #只写模式打开,写入清先清空文件
    'x'       create a new file and open it for writing     #创建一个新文件写
    'a'       open for writing, appending to the end of the file if it exists  #追加写的方式
    'b'       binary mode   #以二进制模式打开
    't'       text mode (default)    #默认以文本模式打开
    '+'       open a disk file for updating (reading and writing)  #
    'U'       universal newline mode (deprecated)
    ========= ===============================================================

通过实例来测试open的用法

fd = open(r"E:file", "w")  #以写的方式打开一个文件

fd.write("Hello world!")     #写入一条数据
Out[22]: 12

fd.close()

fd = open(r"E:file")           #默认是以只读的方式打开文件

fd.read()                             ##
Out[26]: 'Hello world!'

#write与read的进阶
fd.readlines()  #返回的是文件中每一行构成的列表
fd.readline()    #每次返回一行,可以用来迭代

In [5]: fd = open("/etc/passwd")

In [6]: fd.readlines()
Out[6]: 
['root:x:0:0:root:/root:/bin/bash
',
 'bin:x:1:1:bin:/bin:/sbin/nologin
',
 'daemon:x:2:2:daemon:/sbin:/sbin/nologin
',
 'adm:x:3:4:adm:/var/adm:/sbin/nologin
',
 'lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
',
 'sync:x:5:0:sync:/sbin:/bin/sync
',
 'shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
',
 'halt:x:7:0:halt:/sbin:/sbin/halt
',
 'sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
']

In [7]: fd.seek(0)   ##把文件句柄指针指向文件起始位置

In [8]: fd.readline()  #只读一行
Out[8]: 'root:x:0:0:root:/root:/bin/bash
'

#遍历整个文件之一:
In [12]: for i in fd:    
   ....:     print i
#遍历整个文件之二:
In [14]: fd = open("/etc/passwd")

In [15]: while True:
   ....:     line = fd.readline()
   ....:     if not line:
   ....:         break
   ....:     print(line)
   ....: fd.close()

#writelines()的用法
In [16]: fd = open("message")

In [17]: lines = fd.readlines()

In [18]: fd.close()

In [19]: fd = open("message","w")

In [20]: lines
Out[20]: ['Hello world!
', 'I have a dream
', 'Dangerous
']

In [21]: lines.append("Swift
")  #在列表中追加一个字符串

In [22]: fd.writelines(lines)     #写入列表

In [24]: fd.close()

In [31]: fd = open("message")

In [32]: print fd.read()   
Hello world!
I have a dream
Dangerous
Swift

以上默认是使用文本方式打开,在打开文件时使用“b”表示使用二进制的方式打开。

In [33]: fd = open("message")

In [34]: fd.read()      #默认是读取整个文件
Out[34]: 'Hello world!
I have a dream
Dangerous
Swift
'

In [35]: fd.tell()      #函数打印出当前指针的位置
Out[35]: 44

In [36]: fd.seek(0)     #函数可以把句柄指针指向指定的位置

In [37]: fd.read(5)      #可以传入参数,打印多少个字节
Out[37]: 'Hello'

python2与python3的编码格式:

python2中默认使用ASCII编码,python3中使用UNICODE编码。

string------>bytes(encode(encoding="utf-8")) :unicode转为二进制称为编码

bytes------->string(decode(encoding="utf-8")): 二进制转为Unicode称为解码

注意以上只适合于python3

一个关于编码问题的详细解答链接:https://www.cnblogs.com/575dsj/p/7112767.html

原文地址:https://www.cnblogs.com/wxzhe/p/8880837.html