#close是通常选项。调用close会终止外部文件的连接。
#写进文件
myfile = open('myfile.txt', 'w')
myfile.write('hello textfile\n')
myfile.write('goodbye text file\n')
myfile.close()
#读取文件
myfile = open('myfile.txt')
print(myfile.readline())
print(myfile.readline())
print(myfile.readline())
#hello textfile
#goodbye text file
print(open('myfile.txt').read())
#hello textfile
#goodbye text file
#文件迭代器往往是最佳选择
for line in open('myfile.txt'):
print(line,end='')
#hello textfile
#goodbye text file
#python3
#python2
#python3中的区别源自于简单文本和unicode文本并为一种常规的字符串
#因为所有的文本都是unicode,包括ascii和其他8位编码
#文件中处理解析python对象
x, y, z = 43, 44, 45
s = 'spam'
d = {'a':1, 'b':2}
l = [1, 2, 3]
f = open('datafile.txt','w')
f.write(s +'\n')
f.write('%s,%s,%s\n' % (x, y, z))
f.write(str(l) +'$' +str(d) + '\n')
f.close()
chars = open('datafile.txt').read()
print(chars)
#spam
#43,44,45
#[1, 2, 3]${'a': 1, 'b': 2}
f = open('datafile.txt')
line = f.readline()
print(line)
#spam
line.rstrip()
print(line)
#spam
line = f.readline()
print(line)
#43,44,45
parts = line.split(',')
print(parts)
#['43', '44', '45\n']
print(int(parts[1])) # 44
numbers = [int(p) for p in parts]
print(numbers) # [43, 44, 45]
#int和一些其他的转换方法会忽略旁边的空白
line = f.readline()
print(line) # [1, 2, 3]${'a': 1, 'b': 2}
parts = line.split('$')
print(parts) # ['[1, 2, 3]', "{'a': 1, 'b': 2}\n"]
print(eval(parts[0])) # [1, 2, 3]
obj = [eval(p) for p in parts]
print(obj) # [[1, 2, 3], {'a': 1, 'b': 2}]
#用pickle存储python原生对象
d = {'a':1, 'b':2}
f = open('datafile.pkl','wb')
import pickle
pickle.dump(d,f)
f.close()
f = open('datafile.pkl','rb')
e = pickle.load(f)
print(e) # {'a': 1, 'b': 2}
print(open('datafile.pkl','rb').read())
#b'\x80\x03}q\x00(X\x01\x00\x00\x00aq\x01K\x01X\x01\x00\x00\x00bq\x02K\x02u.'
#文件中打包二进制数据的存储于解析
#struct模块能够构造和解析打包的二进制数据
#要生成一个打包的二进制数据文件,用wb模式打开它并将一个格式化字符串和几个python
#对象传给struct,这里用的格式化字符串指一个4字节整数,一个包含4字符的字符串
#以及一个二位整数的数据包。这些都是按照高位在前的形式
f = open('data.bin','wb')
import struct
data = struct.pack('>i4sh',7,b'spam',8)
print(data)
f.write(data)
f.close()
#f = open('data.bin', 'rb')
#data = f.read()
#print(data)
values = struct.unpack('>i4sh',data)
print(values) # (7, b'spam', 8)
#其他文件工具
#标准流,sys模块中预先打开的文件对象如sys.stdout
#os模块中的描述文件
#socket。pipes。FIFO文件
#通过键开存储的文件
#shell流,op.popen和subprocess.Popen
#重访类型分类
#对象根据分类共享操作,如str,list,tuple都共享合并,长度,索引等序列操作
#只有可变对象可以原处修改
#文件导出的唯一方法
#对象分类
#对象类型 分类 是否可变
#bytearray 序列 是
l = ['abc', [(1,2),([3],4)],5]
print(l[1]) # [(1, 2), ([3], 4)]
print(l[1][1]) # ([3], 4)
print(l[1][1][0]) # [3]
#引用vs拷贝
x = [1,2,3]
l = ['a',x,'b']
print(l) # ['a', [1, 2, 3], 'b']
d = {'x':x,'y':2}
print(d) # {'x': [1, 2, 3], 'y': 2}
x[1] = 'surprise'
print(l) # ['a', [1, 'surprise', 3], 'b']
print(d) # {'x': [1, 'surprise', 3], 'y': 2}
x = [1,2,3]
l = ['a',x[:],'b']
print(l) # ['a', [1, 2, 3], 'b']
d = {'x':x[:],'y':2}
print(d) # {'x': [1, 2, 3], 'y': 2}
x[1] = 'surprise'
print(l) # ['a', [1, 2, 3], 'b']
print(d) # {'x': [1, 2, 3], 'y': 2}
import copy
l = [1,2,3]
d = {'a':1,'b':2}
e = l[:]
D = d.copy()
#比较,相等性,真值
l1 = [1,2,4]
l2 = [1,2,4]
print(l1 == l2, l1 is l2) # True False
s1 = 'spam'
s2 = 'spam'
print(s1 == s2, s1 is s2) # True True
a = 'a long strings qqq'
b = 'a long strings qqq'
print(a == b, a is b) # True True ......
d1 = {'a':1,'b':2}
d2 = {'a':1,'b':3}
print(sorted(d1.items()) < sorted(d2.items())) # True
print(sorted(d1.keys()) < sorted(d2.keys())) # False
print(sorted(d1.values()) < sorted(d2.values())) # True
l = [None] *4
print(l) # [None, None, None, None]
print(type([1]) == type([])) # True
print(type([1]) == list) # True
print(isinstance([1],list)) # True
import types
def f():pass
print(type(f) == types.FunctionType) # True
#内置的类型陷阱
#赋值生成引用,而不是拷贝
l = [1,2,3]
m = ['x',l,'y']
print(m) # ['x', [1, 2, 3], 'y']
l[1] = 0
print(m) # ['x', [1, 0, 3], 'y']
#为了避免这种问题,可以用分片来生成一个高级拷贝
l = [1,2,3]
m = ['x',l[:],'y']
l[1] = 0
print(m) # ['x', [1, 2, 3], 'y']
l = [4,5,6]
x = l 3
y = [l] 3
print(x) # [4, 5, 6, 4, 5, 6, 4, 5, 6]
print(y) # [[4, 5, 6], [4, 5, 6], [4, 5, 6]]
l[1] = 0
print(x) # [4, 5, 6, 4, 5, 6, 4, 5, 6]
print(y) # [[4, 0, 6], [4, 0, 6], [4, 0, 6]]
l = ['grail']
l.append(l)
print(l) # ['grail', [...]]
#不可变类型不可再原处修改
t = (1,2,3)
t = t[:2] + (4,)
print(t) # (1, 2, 4)