每天CookBook之Python-076

  • 读写二进制文件
with open('somefile.txt', 'rb') as f:
    data = f.read()

with open('somefile.txt', 'wb') as f:
    f.write(b'Hello World')

t = 'Hello World'

print(t[0])

for c in t:
    print(c)

b = b'Hello World'

print(b[0])

for c in b:
    print(c)

with open('somefile.txt', 'rb') as f:
    data = f.read(16)
    text = data.decode('utf-8')

with open('somefile.txt', 'wb') as f:
    text = 'Hello World'
    f.write(text.encode('utf-8'))

import array

a = array.array('i', [0, 0, 0, 0, 0, 0, 0, 0])
with open('data.txt', 'rb') as f:
    f.readinto(a)

print(a)
posted @ 2016-07-24 12:44  4Thing  阅读(82)  评论(0编辑  收藏  举报