Files Operation in Python

# 基本的文件读写操作
#-------------------------------------------------------------------------------------
output = open(r'D:\text', 'w')       # 创建输出文件用于写操作
input  = open('D:\text', 'r')        # 创建输入文件用于读操作, 第二个参数是默认值,可省略
aString = input.read()               # 读取整个文件到一个字符串
aString = input.read(N)              # 读取接下来的N个字符(字节)到一个字符串
aString = input.readline()           # 读取下一行到一个字符串
aList   = input.realines()           # 读取整个文件到一个字符串列表
output.write(aString)                # 写入一个字符串(字节串)到文件
output.writelines(aList)             # 将列表内所有字符串写入文件
output.close()                       # 手动关闭
output.flush()                       # 将输出缓冲区刷新到磁盘, 但不关闭文件
anyFile.seek(N)                      # 修改文件指针指向偏移量N的位置
for line in open('data'): use line   # 使用文件迭代器逐行读取
open('f.txt', encoding = 'latin-1')  # Python 3.x Unicode文本文件(str字符串)
open('f.bin', 'rb')                  # Python 3.x 字节文件(bytes字符串)
codecs.open('f.txt', encoding = '')  # Python 2.x Unicode文本文件(Unicode字符串)
open('f.bin', 'rb')                  # Python 2.x 字节文件(str字符串)
#-------------------------------------------------------------------------------------

#********************* some tips *************************
# 1. 文件迭代器是最好的读取行工具
# 2. 从文件读取的内容是字符串而不是对象
# 3. 默认情况下文件时缓冲的并且可查找(允许从指定位置读写文件)
# 4. close通常是可选的, 文件在进行收集(collection)之后会自动关闭
#*********************************************************

# 基本文件操作实践
>>> myfile = open('myfile.txt', 'w')
>>> myfile.write('Hello, Andy.\n')  
13
>>> myfile.close()

>>> myfile = open('myfile.txt') # 'r'是默认的打开模式
>>> myfile.readline()
'Hello, Andy.\n'
>>> myfile.readline()
''
>>> myfile.close()

>>> text = open('myfile.txt').read() # 读取整个文件到一个字符串
>>> text
'Hello, Andy.\n'

>>> for line in open('myfile.txt'): #使用文件迭代器
...     print(line, end='')
...
Hello, Andy.
>>>

# Storing Python Objects
>>> X, Y, Z = 43, 44, 45 # Using unpacking
>>> S = 'spam'
>>> D = {'a':97, 'b':98}
>>> L = ['Tom', 'Jerry', 'Andy']
>>> f = open('data', 'w')
>>> f.write('%s,%s,%s\n' % (X, Y, Z))
9
>>> f.write(S + '\n')
5
>>> f.write(str(D) + '$' + str(L) + '\n')
44
>>> f.close()

>>> f = open('data')
>>> f.read()
"43,44,45\nspam\n{'a': 97, 'b': 98}$['Tom', 'Jerry', 'Andy']\n"
>>> f.close()

>>> f = open('data')
>>> line = f.readline()
>>> line
'43,44,45\n'
>>> nums = line.split(',')
>>> nums
['43', '44', '45\n']
>>> listNums = [int(x) for x in nums]
>>> listNums
[43, 44, 45]
>>> X, Y, Z = listNums  # Assignment by seq unpacking

>>> line = f.readline()
>>> line
'spam\n'
>>> S = line.rstrip() # Remove end-of-line
>>> S
'spam'
>>> line = f.readlime()
>>> line
"{'a': 97, 'b': 98}$['Tom', 'Jerry', 'Andy']\n"
>>> parts = line.split('$')
>>> parts
["{'a': 97, 'b': 98}", "['Tom', 'Jerry', 'Andy']\n"]
>>> D = eval(parts[0])  # extract any object from a string, repr do the converse
>>> D
{'a':97, 'b':98}
>>> L = eval(parts[1])
>>> L
['Tom', 'Jerry', 'Andy']

# **************************************************************
# Storing Native Python Objects: Using pickle
>>> D = {'a':97, 'b':98}
>>> f = open('data-pickle', 'wb')
>>> import pickle
>>> pickle.dump(D, f)   # Pickle any object to file
>>> f.close()

>>> f = open('data-pickle', 'rb')
>>> D = pickle.load(f)  # Load any object from file
>>> D
{'a':97, 'b':98}
>>> f.close()

# **************************************************************
# Storing Native Python Objects: Using shelve
>>> D = {'a':97, 'b':98}
>>> L = ['Tom', 'Jerry', 'Andy']
>>> import shelve
>>> db = shelve.open('data-shelve')
>>> db['dict'] = D  # The key here must be strings and shoude be unique 
>>> db['list'] = L
>>> db.close()      # 3 files will be created (data-shelve.bak, data-shelve.dat, data-shelve.dir)

>>> db = shelve.open('data-shelve')
>>> for key in db:
...     print(key, '=>', db[key])
...
list => ['Tom', 'Jerry', 'Andy']
dict => {'a': 97, 'b': 98}
>>> db.close()

# Update db (data-shelve)
>>> db = shelve.open('data-shelve')

# without writeback=true, so D['dict']['c'] = 99 will not work
# https://docs.python.org/2/library/shelve.html#example 
>>> D = db['dict']  # Extract the copy
>>> D['c'] = 99     # mutates the copy
>>> db['dict'] = D  # Store the copy right back 
>>> db.close()

>>> db = shelve.open('data-shelve')
>>> db['dict']
{'a': 97, 'c':99, 'b': 98}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值