python中獲取檔案字尾名的方法

2022-08-23 09:57:08 字數 588 閱讀 1011

獲取檔案的字尾名有好幾種方法:

第一種:splittext()方法

os.path.splittext(path)[-1]

第二種:endswith()方法

path = "test_user_info.py"

bool = path.endswith(".py")

print(bool)

第三種:判斷字尾名是否在字串中(這種會存在誤判,若是.pyx字尾,一樣會列印true,前面兩種不會)

path = "test_user_info.py"

if ".py" in path:

print(true)

第四種:用split方法切割,但是吧這種只是拿到了py沒有點,所以再加上點也是可以的

path = "test_user_info.py"

suffix = path.split(".")[1]

print("suffix: {}".format(suffix))

獲取檔案字尾名

獲取到filename的字尾 利用lastindexof 從最後面截字尾,找到最後乙個點的索引然後加一,利用substring擷取該索引後的字串 f.getfilename substring f.getfilename lastindexof 1 lastindexof 方法有以下四種形式 pub...

獲取檔案的字尾名

使用api pathfindextension實現獲取給定乙個檔案,直接獲取檔案的字尾名 如 檔名 hsduiew.txt jhdsi.adiwey 路徑 檔名 c hsh shsh tetet.txt 都可以直接獲取到字尾名。pathfindextension函式說明 函式原型 ptstr pat...

java獲取檔案字尾名

獲取檔案字尾名 public static string endwith string filename return result 可以放到工程的工具類裡直接呼叫,需要注意的是string token filename.split 一定要用轉義字元,因為spilt裡面有一定的正規表示式在裡面 發現...