Python 獲取當前資料夾下所有檔名

2021-08-08 21:14:34 字數 1087 閱讀 1227

from :

os 模組下有兩個函式:

os.walk()

os.listdir()

#

-*- coding: utf-8 -*- 23

import

os 45

deffile_name(file_dir):

6for root, dirs, files in

os.walk(file_dir):

7print(root) #

當前目錄路徑

8print(dirs) #

當前路徑下所有子目錄

9print(files) #

當前路徑下所有非目錄子檔案

#

-*- coding: utf-8 -*- 23

import

os 45

deffile_name(file_dir):

6 l=

7for root, dirs, files in

os.walk(file_dir):

8for file in

files:

9if os.path.splitext(file)[1] == '

.jpeg

':

1011

return

l 12

1314

#其中os.path.splitext()函式將路徑拆分為檔名+副檔名

#-*- coding: utf-8 -*-

2import

os 34

deflistdir(path, list_name): #傳入儲存的list

5for file in

os.listdir(path):

6 file_path =os.path.join(path, file) 7if

os.path.isdir(file_path):

8listdir(file_path, list_name)

9else

:

利用python 獲取當前資料夾下所有檔名

這裡通過os下的兩個函式實現,os.walk 和os.listdir import os def file name file dir for root,dirs,files in os.walk file dir print root 當前目錄路徑 print dirs 當前路徑下所有子目錄 pr...

python如何獲取當前資料夾下所有檔名詳解

前言 os 模組下有兩個函式 os.walk os.listdir 示例 coding utf 8 iwww.cppcns.commport os def file name file dir for rootwldiiulep,dirs,files in os.walwww.cppcns.comk...

python 獲取當前資料夾下所有檔名

from os 模組下有兩個函式 os.walk os.listdir 1 coding utf 8 23 import os 45 deffile name file dir 6for root,dirs,files in os.walk file dir 7print root 當前目錄路徑 8...