Python中的IO文件操作
一 、常用文件操作
操作方式 解释 r 以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。 rb 以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。 w 打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。 wb 以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。 a 打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。 + 读写方式,可以写入数据,可以读取数据
二 、案例说明
1. 基本操作
file = open ( './data/1.1.text' , 'w' , encoding= 'utf-8' )
message = 'hello 世界'
file . write( message)
file . close( )
file = open ( file = './data/1.1.text' , mode= 'r' , encoding= 'utf-8' )
info = file . read( )
print ( info)
file . close( )
C: \Python37\python. exe E: / PY1901_0114WORK/ days11/ demo01_IO文件操作. py
hello 世界
file = open ( file = './data/1.2.text' , mode= 'a' , encoding= 'UTF-8' )
message = '白日依山尽'
file . write( message)
message = '\n黄河入海流'
file . write( message)
message = '\n欲穷千里目'
file . write( message)
message = '\r更上一层楼'
file . write( message)
file . close( )
白日依山尽
黄河入海流
欲穷千里目
更上一层楼二进制文件的操作
file = open ( file = 'C:/Users/Administrator/Pictures/timg1.jpeg' , mode= 'rb' )
print ( file . read( ) )
file . close( )
file2 = open ( file = './data/test.jpeg' , mode= 'wb' )
file2. write( file . read( ) )
file2. close( )
with open ( 'C:/Users/Administrator/Pictures/skyload.jpg' , 'rb' ) as file1:
with open ( './data/' + file1. name[ file1. name. rfind( '/' ) : ] , 'wb' ) as file2:
file2. write( file1. read( ) )
2. 类型转换和eval函数
users = { 'admin' : { 'username' : 'admin' , 'password' : '123' , 'nickname' : '南易' } }
users_str = str ( users)
with open ( './data/2.1.text' , 'w' ) as file :
file . write( users_str)
with open ( './data/2.1.text' , 'r' ) as file :
users = file . read( )
print ( users, type ( users) )
users = eval ( users)
print ( users, type ( users) )
print ( users. get( 'admin' ) )
C: \Python37\python. exe E: / PY1901_0114WORK/ days11/ demo02_类型转换和eval 函数. py
{ 'admin' : { 'username' : 'admin' , 'password' : '123' , 'nickname' : '南易' } } < class 'str' >
{ 'admin' : { 'username' : 'admin' , 'password' : '123' , 'nickname' : '南易' } } < class 'dict' >
{ 'username' : 'admin' , 'password' : '123' , 'nickname' : '南易' }
3. json模块的数据操作
'''
python中提供一个特殊的模块, 可以直接对python
中的数据进行序列化操作
序列化: 按照指定的数据顺序定义数据格式[类似编码]
'''
import json
users = { 'admin' : { 'username' : 'admin' , 'password' : '123' , 'nickname' : 'MrBean' } }
with open ( './data/3.1.json' , 'w' ) as file :
json. dump( users, file )
with open ( './data/3.1.json' , 'r' ) as file :
users = json. load( file )
print ( users, type ( users) )
C: \Python37\python. exe E: / PY1901_0114WORK/ days11/ demo03_json模块. py
{ 'admin' : { 'username' : 'admin' , 'password' : '123' , 'nickname' : 'MrBean' } } < class 'dict' >
4. marshal操作多个数据
s = '字符串'
i = 10
f = 3.141592653589793
b = True
c = 12 + 5j
l = [ 1 , 2 , 3 ]
d = { 'username' : 'admin' , 'password' : '123' }
x = [ s, i, f, b, c, l, d]
import marshal
with open ( './data/4.1.dat' , 'wb' ) as file :
marshal. dump( len ( x) , file )
for x1 in x:
marshal. dump( x1, file )
with open ( './data/4.1.dat' , 'rb' ) as file :
n = marshal. load( file )
for x in range ( n) :
print ( marshal. load( file ) )
字符串
10
3.141592653589793
True
( 12 + 5j )
[ 1 , 2 , 3 ]
{ 'username' : 'admin' , 'password' : '123' }
5. shelve操作多个数据
users = { 'admin' : { 'username' : 'admin' , 'password' : '123' , 'nickname' : 'MrBean' } }
articles = { '标题' : { 'title' : '标题' , 'content' : '文章内容' , 'author' : users[ 'admin' ] } }
import shelve
file = shelve. open ( './data/5.1' )
file [ 'users' ] = users
file [ 'articles' ] = articles
print ( file [ 'users' ] , type ( file [ 'users' ] ) )
print ( file [ 'articles' ] , type ( file [ 'articles' ] ) )
C: \Python37\python. exe E: / PY1901_0114WORK/ days11/ demo05_shelve. py
{ 'admin' : { 'username' : 'admin' , 'password' : '123' , 'nickname' : 'MrBean' } } < class 'dict' >
{ '标题' : { 'title' : '标题' , 'content' : '文章内容' , 'author' : { 'username' : 'admin' , 'password' : '123' , 'nickname' : 'MrBean' } } } < class 'dict' >
小结:
python存取多个数据到文件中
如果按常规方式-open/write/read方式, 每个独立的数据都要单独存储一个文件
1. str()数据: 字符串:文件存储-->文件读取:eval(): 数据
2. 数据: json.dump(): 文件存储->json.load():数据
如果要存储多个数据到一个文件中
3. 多个数据: 总数量:marshal.dump()->marshal.load(fp):
总数量:循环->marshal.load(fp)依次读取
多个数据, 也可以按照比较友好的like dict的方式, 将数据存储到文件中
可以将数据存储在文件中, 通过key值直接获取对应的数据,用shelve