#向文件中读取和写入数据
with open('cookbook_p142.txt','wt') as f:
f.write('text1\n')
f.write('text2\n')
print('text3',file=f)
with open('cookbook_p142.txt','rt') as f:
# print(f.read())
# print(f.read())
for line in f.read():
print(line)
print(type(f))
print(type(f.read()))
with open('cookbook_p142.txt','rt') as f:
for line in f:
print(line)
with open('cookbook_p142.txt', 'at') as f:
print('tst',file=f)
for i in range(5):
print(i, end=" ***\n" )
ii = []
for i in range(5):
ii.append(i)
print('\n'.join((str(k) for k in ii)))
print(''.join(['1','2','4']))
a = [str(k) for k in ii]
print(a)
import array
temp = array.array('i',[1,2,3,4])
print(temp)
with open('cookbook_p142.txt','ab') as f:
f.write(temp)
with open('cookbook_p142.txt','rb') as f:
for line in f :
print(line)
print(temp.itemsize)
D:\pythonWork\venv\Scripts\python.exe D:/pythonWork/cookbook_p142.py
t
e
x
t
1
t
e
x
t
2
t
e
x
t
3
<class '_io.TextIOWrapper'>
<class 'str'>
text1
text2
text3
0 ***
1 ***
2 ***
3 ***
4 ***
0
1
2
3
4
124
['0', '1', '2', '3', '4']
array('i', [1, 2, 3, 4])
b'text1\r\n'
b'text2\r\n'
b'text3\r\n'
b'tst\r\n'
b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00'
4
Process finished with exit code 0