pyhton IO

#向文件中读取和写入数据
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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值