# 基本的文件读写操作
#-------------------------------------------------------------------------------------
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}

2002

被折叠的 条评论
为什么被折叠?



