1. 程式人生 > 實用技巧 >LCP12.小張刷題計劃

LCP12.小張刷題計劃

檔案
python檔案,word檔案,音訊檔案,圖片檔案

.py
.docx
.xlsx
.pptx
.mp4
.png

檔案的作用
資料持久化:利用檔案來儲存軟體執行過程中的資料,並且可以通過對檔案中的資料進行讀寫

檔案中常用的操作
    1.開啟檔案
file = open("url","w",encoding="utf-8")

r:只讀
w:寫入,檔名存在,重新建立覆蓋後寫入;檔名不存在,建立後寫入
a:追加寫入,檔名存在,在檔案內容最後進行寫入操作;檔名不存在,建立後寫入
2.讀/寫

寫入檔案
write("寫入內容")

讀取檔案
read():一次性讀取所有的,並且儲存資料型別為str
            readline():只能讀取一行資料,並且儲存資料型別為str
            readlines():一下讀取所有的行,並且儲存資料型別為list列表

3.關閉檔案
file.close()

例項1:
將"HelloWorld"寫入到指定的檔案中123.txt
# 1.開啟檔案
# 編碼方式預設為cp936(GBK)     手動指定為utf-8
file = open("
D:/python/PyCharmProjects/07_遞迴及檔案處理/123.txt","w",encoding="utf-8") # 2.讀/寫 file.write("HelloWorld") # 3.關閉檔案 file.close()

例項2:
將你最喜歡的一首古詩寫入指定的檔案,注意換行
# 建立一個寫入函式,將古詩寫入指定檔案poetry.txt
def write_poetry():
    # 1.開啟檔案
    file = open("D:/python/PyCharmProjects/07_遞迴及檔案處理/poetry.txt","a",encoding="utf-8")

    
for i in range(1,5): content = input("請輸入第%d行古詩:"%i) # 2.讀/寫 file.write(content+"\n") # 檔案重新整理操作 file.flush() print("古詩寫入成功!") # 3.關閉檔案的時候,預設重新整理 file.close() # 呼叫寫入函式 write_poetry()
# 檔案的讀取操作
# 1.第一種read()讀取方式,一次性讀取所有的,並且儲存資料型別為str
def read_poetry():
    # 1.開啟檔案
    file = open("D:/python/PyCharmProjects/07_遞迴及檔案處理/poetry.txt", "r", encoding="utf-8")
    # 2.讀取檔案
    if file.readable():
        # 判斷檔案是否可讀
        content = file.read()
        print(content)
        print("資料型別:",type(content))
    # 3.關閉檔案
    file.close()

read_poetry()

# 檔案的讀取操作
# 2.第二種readline()讀取方式,只能讀取一行資料,並且儲存資料型別為str
def read_poetry_2():
    # 1.開啟檔案
    file = open("D:/python/PyCharmProjects/07_遞迴及檔案處理/poetry.txt", "r", encoding="utf-8")
    # 2.讀取檔案
    if file.readable():
        # 判斷檔案是否可讀
        content = file.readline()
        # 這一步是為了將,每一行古詩後面的換行去掉
        content = content[:-1]
        while content != "":
            print(content)
            print("資料型別:",type(content))
            content = file.readline()
            content = content[:-1]
    # 3.關閉檔案
    file.close()

read_poetry_2()

# 檔案的讀取操作 
# 3.第三種readlines(),一下讀取所有的行,並且儲存資料型別為list列表
def read_poetry_3():
    # 1.開啟檔案
    file = open("D:/python/PyCharmProjects/07_遞迴及檔案處理/poetry.txt", "r", encoding="utf-8")
    # 2.讀取檔案
    if file.readable():
        # 判斷檔案是否可讀
        content = file.readlines()
        print(content)
        print("資料型別:",type(content))
    # 3.關閉檔案
    file.close()

read_poetry_3()

例項3

複製目標檔案123.txt,在當前檔案目錄下建立一個副本123-副本.txt

"""
切片

字串[start:end:step]

切片操作:
    包括start,不包括end



"""
# 複製目標檔案,檔案為srcFile(表示檔案的完整路徑/絕對路徑)
# D:/python/PyCharmProjects/08_檔案/123.txt
def copy_file(srcFile):
    # 1.開啟目標檔案
    file = open(srcFile,"r",encoding="utf-8")
    # 2.讀取檔案內容
    content = file.read()

    # 3.寫入新檔案
    # 建立新的檔案,並對名字進行處理:原檔名-副本.txt
    # 3_1 在檔名中,從右往左找到"/"的位置,並提取"123.txt
    index = srcFile.rindex("/")
    file_name = srcFile[index+1::]
    # 3_2 在file_name中,從右往左找到"."的位置,並分離出"123"和".txt"
    index_1 = file_name.index(".")
    name_1 = file_name[0:index_1]
    name_2 = file_name[index_1::]

    # 新的檔名字
    new_file_name = name_1 + "-副本" + name_2
    new_file_path = srcFile[:index+1] + new_file_name
    new_file = open(new_file_path,"w",encoding="utf-8")
    # 將讀取到的內容,寫入到新檔案中
    new_file.write(content)

    # 4.關閉檔案
    file.close()
    new_file.close()

copy_file("D:/python/PyCharmProjects/08_檔案/123.txt")