10、檔案處理
阿新 • • 發佈:2021-09-28
with open(r'a.txt',mode='wt',encoding='utf-8') as f:
f.write("你好")
with open(r'a.txt',mode='ab') as f:
f.write("哈哈哈".encode('utf-8'))
with open(r'a.txt',mode='rb') as f:
data = f.read()
print(data.decode('utf-8'))
with open(r'1.mp4',mode='rb') as f:
data = f.read()
print(data)
for line in f:
print(line)
''
with open('1.mp4',mode='rb') as f1,open(r'D:\111111111111.mp4',mode='wb') as f2:
data = f1.read()
f2.write(data)
for line in f1:
f2.write(line)
二、可讀可寫模式
with open('a.txt',mode='r+t',encoding='utf-8') as f:
f.write('次臥')
f.write('h')
三、修改檔案的兩種方式
with open('123.txt',mode='wb') as f:
pass
with open('123.mp4',mode='rb') as f1,open('456.mp4',mode='wb') as f2:
for line in f1:
f2.write(line)
with open('123.txt',mode='rt',encoding='utf-8') as f1:
date = f1.read()
res = date.replace('wyf','WYF')
with open('123.txt',mode='wt') as f1:
f1.write(res)
import os
with open('123.txt',mode='rt',encoding='utf-8') as f1,open('.123.txt.swp',mode='wt') as f2:
date = f1.read()
res = date.replace('WYF','wyf')
f2.write(res)
os.remove('123.txt')
os.rename('.123.txt.swp','123.txt')
inpu_user = input("請輸入你的使用者名稱:")
inpu_pwd = input("請輸入你的密碼:")
inpu_money = input("請輸入你的餘額:")
with open('123.txt',mode='at') as f:
f.write('%s:%s:%s\n' %(inpu_user,inpu_pwd,inpu_money))
import os
with open('123.txt',mode='rt',encoding='utf-8') as f1,open('.123.txt.swp',mode='wt') as f2:
for line in f1:
user,pwd,money = line.strip("\n").split(":")
if user == "wyf":
money = int(money)-100
elif user == "cxk":
money = int(money)+100
line = '%s:%s:%s\n'%(user,pwd,money)
f2.write(line)
os.remove('123.txt')
os.rename('.123.txt.swp','123.txt')
with open("198.txt",mode="wt") as f:
print(f.writable())
f.write('hcuwehfui')
with open("198.txt",mode='rt',encoding='utf-8') as f:
print(f.readable())
print(f.readline())
四、練習(註冊和轉賬)
註冊
inp_user = input("請輸入您的用名:").strip()
inp_pwd = input("請輸入您的密碼:").strip()
inp_money = input("請輸入您的餘額:").strip()
with open('db.txt', mode='at', encoding='utf-8') as f:
f.write('%s:%s:%s\n' % (inp_user, inp_pwd, inp_money))
轉賬
import os
with open('db.txt',mode='rt',encoding='utf-8') as f1,open('.db.txt.swp',mode='wt',encoding='utf-8') as f2:
for line in f1:
user,pwd,money = line.strip('\n').split(':')
if user == 'tom':
money = int(money) - 100
elif user == 'lili':
money = int(money) + 100
line = "%s:%s:%s\n" % (user, pwd, money)
f2.write(line)
os.remove('db.txt')
os.rename('.db.txt.swp','db.txt')
五、檔案讀寫的其他方法
with open('b.txt',mode='rt',encoding='utf-8') as f:
print(f.readable()) 是否可讀
print(f.readline()) 讀取一行內容,游標移動到第二行首部
print(f.readlines()) 讀取每一行內容,存放於列表中
lines = f.readlines()
print(lines[0])
with open('b.txt', mode='wt', encoding='utf-8') as f:
print(f.writable())
lines = ['你好\n', '我好\n', '大家好\n']
for line in lines:
f.write(line)
f.writelines(lines)
f.write('1111\n222\n333\n')
f.write('hello')
f.writelines('hello')
f.flush() 立刻將檔案內容從記憶體刷到硬碟
with open('b.txt', mode='at', encoding='utf-8') as f:
f.truncate(3)
六、控制檔案指標移動
儲備知識:只有t模式下的read(n)的n代表的是字元個數,除此以外都代表位元組個數
with open('b.txt',mode='rt',encoding='utf-8') as f:
print(f.read(4)) 從第4個字元的位置讀到檔案末尾
with open('b.txt',mode='rb') as f:
print(f.read(3).decode('utf-8'))
f.seek(移動的位元組個數,模式)
模式:
0:參照檔案開頭,只有0模式可以在t下使用,其他的都只能在b下使用
with open('b.txt',mode='rt',encoding='utf-8') as f:
f.seek(3,0)
f.seek(6,0)
print(f.tell()) 檢視當前指標距離檔案開頭的位置
1:參照指標當前所在的位置,只能在b下使用
with open('b.txt',mode='rb') as f:
f.seek(3,1)
f.seek(6,1)
print(f.tell())
2:參照指標檔案末尾,通常是倒著移動,只能在b下使用
with open('b.txt',mode='rb') as f:
f.seek(-5,2)
f.seek(-8,2)
print(f.tell())
print(f.read().decode('utf-8'))