python 遞迴讀取檔案

2021-08-22 16:11:15 字數 482 閱讀 4263

廢話不多說,直接上**,注意路徑傳引數的時候需要標註(r)

import os

#遞迴遍歷目錄

def getalldirindigui( path):

fileslist=os.listdir(path)

print(fileslist)

for filename in fileslist:

fileabpath=os.path.join(path,filename)

if os.path.isdir(fileabpath):

print("目錄:" ,filename)

getalldirindigui(fileabpath)

else:

print("普通檔案",filename)

#傳遞引數的時候,注意需要寫上(r)表明傳遞的是路徑

getalldirindigui(r"d:\test")

python高階讀取檔案 Python讀取檔案內容

開啟檔案之後,就可以讀取檔案的內容,檔案物件提供多種讀取檔案內容的方法。開啟test.txt檔案 f open test.txt r 開啟test.txt檔案 f.close 關閉檔案 test.txt檔案有以下內容 hello world.hello python.hello imooc.讀取若干...

Python檔案讀取

python提供了多種方法實現檔案讀取操作 1 read 2 readline 3 readlines 4 xreadlines 很多人也在糾結到底應該選擇哪種方式,甚至疑問在處理大檔案時應該選擇哪種方式,因為擔心檔案過大導致記憶體佔用率過高甚至無法完全載入。其實,這個問題是多餘的,在引入了迭代器和...

python檔案讀取

1.讀取txt檔案 read 讀取整行檔案 readline 讀取一行資料 readines 讀取所有行的資料 讀取txt檔案 user file open user info.txt r lines user file.readlines forline inlines username line...