with open 讀寫文件簡單操作


# 讀文件

# 開始准備要讀取的文件
    with open('11.txt','rb') as f:
         # 讀的方式是讀所有行
         r = f.readlines()

         # print (r)




# # 寫文件# 開始寫入這個文件
    with open('22.txt','w') as f:
          # 寫入的內容
          f.write('hello python')



# 從一個文件讀取數據到另外一個文件
    with open('11.txt','rb') as f:
          a=f.readlines()
          b=str(a)[3:-2:]
          print(b)

          with open('22.txt','w') as j:
            # 追加寫
           # with open('22.txt','a') as j:
                  # j.write(names+"\n")

                 j.write(str(b))
                 # print(b)

# 讀取圖片文件,寫入圖片文件
    with open('1.png','rb') as f:
          c=f.read()
          # print(c)
          with open('2.png','wb') as j:
                j.write(c)

剛開始 使用園子寫博客,還不太會用,代碼格式可能有點錯亂,需要調整。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM