python練習 讀寫檔案

2021-09-17 21:06:41 字數 666 閱讀 1721

對於不同的作業系統,檔案目錄會有不同,對於linux而言,「萬事萬物皆檔案」,對windows而言,檔案讀寫也很重要。

import os

#拼接路徑

os.path.

join

('usr'

,'bin'

,'spam'

)#返回路徑:'usr\\bin\\spam'兩個倒斜槓等價於乙個正斜槓

#獲得當前工作目錄

os.getcmd()

#相對路徑.

.\#絕對路徑c

:\(就是要指明碟符)

#檢視資料夾大小和資料夾內容

os.path.

getsize

(path)

#返回路徑

os.path.

listdir

(path)

hellofile =

open

('路徑'

)#唯讀模式加引數'r'

hellocontent = hellofile.

read()

some = hellofile.

readlines

()#按行讀取

some.write()

python 檔案讀寫練習

練習包括 讀取與寫入 usr bin env python coding utf 8 time 2017 10 30 0030 08 58 file lianxi2.py 正向排序 import codecs l 1,5,3,8,2,4 l.sort 寫入檔案 with codecs.open 1....

python讀寫檔案練習

1.請你通過檔案讀寫命令,讀取 photo1 裡的資料 提示見 區開頭 然後,新建名為 photo2 的 在同乙個資料夾 寫入讀到的資料。這樣,我們就通過檔案讀寫的 完成了的複製 而非滑鼠右鍵 with open photo2.png rb as file 以 rb 模式開啟 data file.r...

1 python檔案讀寫練習

今天學習了python的檔案讀寫功能,練習一下。開啟原日誌檔案 fp open r log.txt 建立新檔案用於存放日誌整理後的結果 fs open r result.txt w 宣告乙個字典,用於存放日誌裡的內容 d 遍歷原日誌檔案的 for line in fp.readlines 如果讀取的...