python 檔案管理os模組應用

2021-09-14 02:31:39 字數 358 閱讀 5632

os.walk(top, topdown=true, onerror=none, followlinks=false)
可以得到乙個三元tupple(dirpath, dirnames, filenames), 

第乙個為起始路徑,第二個為起始路徑下的資料夾,第三個是起始路徑下的檔案。

dirpath 是乙個string,代表目錄的路徑,

dirnames 是乙個list,包含了dirpath下所有子目錄的名字。

filenames 是乙個list,包含了非目錄檔案的名字。

這些名字不包含路徑資訊,如果需要得到全路徑,需要使用os.path.join(dirpath, name)

檔案OS模組

檔案os模組 開啟檔案open file open file name路徑,訪問模式 r 以唯讀的方式開啟 rb 以二進位制的方式開啟乙個檔案用於唯讀 w 開啟乙個檔案只用於寫入 a 追加 在當前路徑下建立乙個文字檔案 file1 open 1.txt w 注意 對檔案操作完後,一定要記得關閉檔案,...

python模組 OS模組

bin env python coding utf 8 import os print os.name 輸出主機平台 print os.getcwd 輸出當前目錄 print os.listdir os.getcwd 輸出當前目錄的檔案 橫向 for i in os.listdir os.getcw...

python 模組 OS模組

print os.getcwd 輸出 e python workspace 原來 print os.getcwd 輸出 e python workspace 返回上級目錄 os.chdir os.getcwd 輸出 e python 更改 os.chdir r e print os.getcwd 輸...