inp_file = input("請輸入需要複製檔案的路徑:
").strip()
new_file = input("
需要建立檔案副本位置:
").strip()
with open(rf
"",mode="rb"
) as f,\
open(rf
"",mode="wb"
) as f1:
while
true:
res = f.read(1024)
if len(res) ==0:
break
f1.write(res)
#r+模式實現先讀後寫再讀
with open("
a.txt
",mode="
r+b"
) as f:
while
true:
res =f.readline()
ifnot
res:
break
print(res.decode("
utf-8
"),end=""
)
() f.write(b
"12345\n")
f.seek(0,0)
while
true:
res =f.readline()
ifnot
res:
break
print(res.decode("
utf-8
"), end="")#
r+模式實現先寫後讀
with open("
a.txt
",mode="
r+b"
) as f:
f.seek(0,2) #
此處指標跳到末尾,不可使用t模式,只能使用b模式
#t模式下,只能seek函式只能使用模式0,即以檔案開頭為參照物
f.write(b"
12345\n")
f.seek(0)
#不設定模式,預設為0模式,即指標以檔案頭為參照物
while
true:
res =f.readline()
ifnot
res:
break
print(res.decode("
utf-8
"), end="")#
w+模式實現讀寫
with open("
b.txt
",mode="
w+b"
) as f:
f.write(b
"12345\n")
f.seek(0,0)
while
true:
res =f.readline()
ifnot
res:
break
print(res.decode("
utf-8
"), end="")#
a+模式實現讀寫
with open("
b.txt
",mode="
a+b"
) as f:
f.write(
"你妹!
".encode("
utf-8"))
f.seek(0,0)
#指標移動的單位都是以bytes/位元組為單位
#只有一種情況特殊:
#t模式下的read(n),n代表的是字元個數
#f.seek(7,0) # 當取第七個字元時,中文字元編碼被截斷,將無法正確讀取。'utf-8' codec can't decode byte 0xbd in position 0: invalid start byte
while
true:
res =f.readline()
ifnot
res:
break
print(res.decode("
utf-8
"), end="")
importtime
with open(
"access.log
",mode="
a+b"
) as f:
while
true:
line =f.readline()
ifline:
print(line.decode("
utf-8
"),end=""
)
else
: time.sleep(0.05)
C 大學教程練習(2)檔案處理
include include using namespace std class phonenumber ostream operator ostream output,phonenumber phone istream operator istream input,phonenumber pho...
2020Python練習七 檔案處理2
2020python練習七 檔案處理2 2020.3.15 週末綜合作業 username1 input 請輸入你的名字 strip usercode1 input 請輸入你的密碼 strip count 0 with open r d 0tempt db.txt mode rt encoding ...
perl學習(2)檔案處理
1 讀取某檔案,如果該檔案不存在,則報錯,並提示出錯原因 open db,home ellie myfile or die can t open file n 2 讀寫檔案的方法 open fh,opens filename for reading.讀 the symbol is optional....