python讀取資料夾內容

2021-10-21 01:42:40 字數 925 閱讀 3239

os.listdir() 方法用於返回指定的資料夾包含的檔案或資料夾的名字的列表

import os, sys

# 開啟檔案

cooked_folder = './cooked_traces/' #資料夾的位址

dirs = os.listdir( cooked_folder )

# 輸出所有檔案和資料夾

for file in dirs:

print (file) #讀出所有資料夾名字

open(file, moopen(file, mode='r', buffering=-1, encoding=none, errors=none, newline=none, closefd=true, opener=none)
#具體的每個關鍵字的含義

python open() 方法用於開啟乙個檔案,並返回檔案物件,在對檔案進行處理過程都需要使用到這個函式,如果該檔案無法被開啟,會丟擲 oserror。

import os, sys

# 開啟檔案

cooked_folder = './cooked_traces/' #資料夾的位址

dirs = os.listdir( cooked_folder )

# 輸出所有檔案和資料夾

for file in dirs:

# print(file) #輸出所有資料夾名字

filepath = cooked_folder + file #檔案所在位址

with open(filepath,'r') as f: #讀取檔案

for line in f : # #按行遍歷檔案內容

print(line) #輸出每行資訊

php讀取資料夾內檔案及資料夾

php讀取資料夾內檔案及資料夾 引數 資料夾路徑 dir,格式要求 資料夾名稱後必須有 返回值 含有檔名稱和路徑的一維陣列 function read dir else 進行路徑拼接 allfile array merge file arr,dir arr 拼合陣列 return allfile f...

python讀取指定資料夾內所有檔案

起因 在寫python傳送測試報告的郵件時,需要從多個json檔案讀取資料 解決辦法 python os模組下的walk 函式可對指定目錄下的資料夾 檔案進行遍歷 result list for root,dirs,files in os.walk path for name in files wa...

python遍歷資料夾讀取檔案大小

閒來無事,寫了個小程式刪除記憶體卡中大於50m的檔案 filename itertaorfilefolder import os import os.path filepath raw input enter filepath 遍歷資料夾 三個引數 分別返回1.父目錄 2.所有資料夾名字 不含路徑 ...