1. 程式人生 > 實用技巧 >python --檔案操作模式詳解

python --檔案操作模式詳解

#f= open(r"aaa/a.txt",mode='rt') # f的值是一種變數,佔用的是應用程式的記憶體空間,此時牽扯的是兩個方面的資源
#print(f)

##2.操作檔案:讀/寫檔案,應用程式對檔案的讀寫請求都是向作業系統呼叫,然後由作業系統控制把硬碟把輸入讀入記憶體,或者寫入硬碟
#res=f.read()
#print(res)

#關閉檔案 z
#f.close()"""回收作業系統資源,f 還存在"""
#f變數還存在,但是不能再讀了
#del f """回收應用程式程式資源"""


#檔案物件又稱為檔案控制代碼
# with open("a.txt",mode='rt') as f1:
# res=f1.read()
# print(res)

# with open("a.txt",mode='rt') as f1,open("b.txt",mode='rt') as f2:
# res1=f1.read()
# res2=f2.read()
# print(res1)
# print(res2)

"""指定字元編碼"""
"""t 文字(預設的模式)
1。 讀寫都是以str (unicode)為單位的
2。文字檔案
3。必須指定encoding='utf-8'"""

# 沒有指定encoding 引數作業系統會使用自己的預設的編碼,
# Linux 和mac 預設使用utf-8
# Windows預設gbk
# with open ('c.txt',mode='rt',encoding='utf-8') as f:
# res=f.read() #t模式會將f.read()讀出來的結果解碼成unicode
# print(res,type(res))
#
#記憶體:utf-8格式的二進位制----解碼--->unicode
#硬碟(c.txt 內容 utf-8格式的二進位制)

#以t 模式為基礎進行記憶體操作
##1.rt

在本python檔案的目錄下建立一個name.txt的文字檔案,
izhan:1005

inp_username = input("please input your name:").strip()
inp_password = input("please input your password:").strip()
 with open("name.txt",mode='rt',encoding='utf-8') as f:
     res=f.read()
     print(res)
     username,password=res.split(":")
     print(username)
     print(password)
 print(inp_username)
 
print(inp_password) if inp_username == username and inp_password == password: print("congratulations! you can login") else: print("sorry! the password is wrong or has no this user")
please input your name:izhan
please input your password:1005
izhan
1005
izhan
1005
congratulations! you can login

隨便輸入看看:
please input your name:dandan
please input your password:
0711 izhan 1005 dandan 0711 sorry! the password is wrong or has no this user Process finished with exit code 0

再建立一個name.txt 檔案

izhan:1005
lili:1111
dandan:0711
另外:python spit 的學習:

https://www.cnblogs.com/clairedandan/p/10926173.html

inp_username = input("please input your name:").strip()
inp_password = input("please input your password:").strip()

with open("name.txt",mode='rt',encoding='utf-8') as f:
    for line in f:
        print(line,end='') # 沒有end='',結果就會一行一行中間有一行空的。
        username,password=line.strip().split(":")
        # print(username)
        # print(password)
        if inp_username == username and inp_password == password:
            print("login successfully")
            break
    else:
        print("賬號錯誤")
please input your name:lili
please input your password:0711
izhan:1005
lili:1111
dandan:0711賬號錯誤

Process finished with exit code 0

please input your name:izhan
please input your password:1005
izhan:1005
login successfully

Process finished with exit code 0

"""w:只寫模式,當檔案不存在時會創造空檔案,當檔案存在時會清空檔案,"""
# with open("d.txt",mode="wt",encoding="utf-8") as f:
# # f.read()"""報錯,不可讀"""
# f.write("hahaha\n")
# f.write("hello\n")
# f.write("wwwwww\n")
##如果重新開啟檔案,則會清空之前的內容,如果連續write,則會連續寫的

"""3.a 只追加寫,"""
# with open("a.txt",mode="at",encoding="utf-8") as f:
# # f.read() 不能讀
# f.write("hahahahah\n")
# f.write("enenenenen\n")
# f.write("oooooo\n")

"""a 重新開啟檔案,不會清空檔案內容,會將檔案指標直接移動到末尾
a 檔案一直用作記錄日誌,註冊功能"""
"""a 模式註冊功能"""
# name=input("please input your name:")
# pwd=input("please input your password:")
# with open("db.txt",mode="at",encoding="utf-8") as f:
# f.write("{}:{}\n".format(name,pwd))

w 可以用做文字檔案的copy

with open("e.txt",mode="rt",encoding="utf=8")as f1,\
    open("f.txt",mode="wt",encoding="utf-8")as f2:
    res=f1.read()
    f2.write(res)
src_file=input("原始檔檔案:")
dst_file=input("目標檔案:")
with open(r"{}".format(src_file),mode="rt",encoding="utf=8")as f1,\
    open(r"{}".format(dst_file),mode="wt",encoding="utf-8")as f2:
    res=f1.read()
    f2.write(res)
/usr/local/bin/python3.8 /Users/futantan/PycharmProjects/S14/檔案知識/檔案處理.py
原始檔檔案:/Users/futantan/PycharmProjects/S14/檔案知識/a.txt
目標檔案:/Users/futantan/PycharmProjects/S14/a_copy.txt

Process finished with exit code 0

此時就可以發現有一個copy的檔案了