Python封裝的獲取檔案目錄的函式

2022-07-09 10:00:10 字數 4412 閱讀 6255

import os

#filepath 輸入資料夾全路徑

#mode

# 1遞迴獲取所有檔名;

# 2遞迴獲取所有檔案路徑;

# 3獲取direct檔名;

# 4獲取direct檔案路徑;

# 5獲取direct檔名和direct子資料夾名;

# 6獲取direct檔案路徑和direct子資料夾路徑

def getfile(filepath, mode, type):

listresult=

if mode == 1:

for parent, dirnames, filenames in os.walk(rootdir):

for filename in filenames:

if type !="" and not filename.endswith(type):

continue

elif mode == 2:

for parent, dirnames, filenames in os.walk(rootdir):

for filename in filenames:

if type !="" and not filename.endswith(type):

continue

elif mode == 3:

listfiletitle = os.listdir(filepath)

for each in listfiletitle:

eachfilepath = os.path.join(filepath, each)

if os.path.isfile(eachfilepath):

if(type !="" and not eachfilepath.endswith(type)):

continue

elif mode == 4:

listfiletitle = os.listdir(filepath)

for each in listfiletitle:

eachfilepath = os.path.join(filepath, each)

if os.path.isfile(eachfilepath):

if type !="" and not eachfilepath.endswith(type):

continue

elif mode == 5:

listtemp=os.listdir(filepath)

for each in listtemp:

eachfilepath = os.path.join(filepath, each)

if (os.path.isfile(eachfilepath) and type !="" and not eachfilepath.endswith(type)):

continue #是檔案#指定了字尾#不是指定的字尾

elif mode == 6:

listfiletitle = os.listdir(filepath)

for eachtitle in listfiletitle:

eachfilepath=os.path.join(filepath, eachtitle)

if (os.path.isfile(eachfilepath) and type !="" and not eachfilepath.endswith(type)):

continue #是檔案#指定了字尾#不是指定的字尾

return listresult

rootdir = "d:\\test"

outpath = "d:\\pytest.txt"

filewriter = open(outpath, 'w')

for each in range(1,7,1):

filewriter.write('\n')

filewriter.write("mode==")

filewriter.write(str(each))

filewriter.write('\n')

listfile=getfile(rootdir, each, '.ppt')

for each in listfile:

filewriter.write(each+'\n')

filewriter.close()

建議自己碼一遍,不想碼?拿走別客氣

import os

#filepath 輸入資料夾全路徑

#mode

# 1遞迴獲取所有檔名;

# 2遞迴獲取所有檔案路徑;

# 3獲取direct檔名;

# 4獲取direct檔案路徑;

# 5獲取direct檔名和direct子資料夾名;

# 6獲取direct檔案路徑和direct子資料夾路徑

def getfile(filepath, mode, type):

listresult=

if mode == 1:

for parent, dirnames, filenames in

os.walk(rootdir):

for filename in filenames:

iftype !=""

andnot filename.endswith(type):

continue

elif mode == 2:

for parent, dirnames, filenames in

os.walk(rootdir):

for filename in filenames:

iftype !=""

andnot filename.endswith(type):

continue

elif mode == 3:

listfiletitle = os.listdir(filepath)

for each in listfiletitle:

eachfilepath = os.path.join(filepath, each)

ifos.path.isfile(eachfilepath):

if(type !=""

andnot eachfilepath.endswith(type)):

continue

elif mode == 4:

listfiletitle = os.listdir(filepath)

for each in listfiletitle:

eachfilepath = os.path.join(filepath, each)

ifos.path.isfile(eachfilepath):

iftype !=""

andnot eachfilepath.endswith(type):

continue

elif mode == 5:

listtemp=os.listdir(filepath)

for each in listtemp:

eachfilepath = os.path.join(filepath, each)

if (os.path.isfile(eachfilepath) and

type !=""

andnot eachfilepath.endswith(type)):

continue #是檔案#指定了字尾#不是指定的字尾

elif mode == 6:

listfiletitle = os.listdir(filepath)

for eachtitle in listfiletitle:

eachfilepath=os.path.join(filepath, eachtitle)

if (os.path.isfile(eachfilepath) and

type !=""

andnot eachfilepath.endswith(type)):

continue #是檔案#指定了字尾#不是指定的字尾

return listresult

rootdir = "d:\\test"

outpath = "d:\\pytest.txt"

filewriter = open(outpath, 'w')

for each in range(1,7,1):

filewriter.write('\n')

filewriter.write("mode==")

filewriter.write(str(each))

filewriter.write('\n')

listfile=getfile(rootdir, each, '.ppt')

for each in listfile:

filewriter.write(each+'\n')

filewriter.close()

Python 獲取檔案目錄

cwd os.getcwd 獲取檔案路徑 print cwd print os.path.basename cwd print os.path.dirname cwd basename返回path最後的檔名。如果path以 或 結尾,那麼就會返回空值。os.path.basename c test....

python 獲取當前目錄下的檔案目錄和檔名

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 當前目錄路徑 8print ...

python 獲取專案根目錄路徑工具類封裝

專案很多地方都用了路徑變數或引數,部署在不同裝置上專案各檔案的相對路徑雖沒有變,但絕對路徑卻始終在變化,簡單粗暴的方式可以find專案名字獲取專案路徑拼接獲取專案根目錄和其他檔案目錄,但希望可以有更文明的方式去獲取專案的根目錄,進而拼接相對路徑,獲取到絕對路徑。usr bin python3 cod...