導航:首頁 > 編程語言 > python獲取當前方法名

python獲取當前方法名

發布時間:2023-08-04 20:15:43

python怎樣獲取當前打開的文件的文件名

import os.path

f1 = open(m,'r') #打開文件

filename=os.path.basename(f1.name) #用f1.name得到文件路徑,os.path.basename得到文件名

Ⅱ python獲取指定目錄下所有文件名列表的方法


本文實例講述了python獲取指定目錄下所有文件名列表的方法。分享給大家供嫌返廳大家參考。具體實現方法如下:世攔
這里python代碼實現獲取文件名列表的功能,可以指定文件中包含的字元,方便提取特定類型的文件名列表:
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
#~ #------------------------------------------------------------------
#~ mole:wlab
#~ Filename:wgetfilelist.py
#~ Function :
#~ def IsSubString(SubStrList,Str)
#~ def GetFileList(FindPath,FlagStr=[]):
#~ 功能:讀取芹隱指定目錄下特定類型的文件名列表
#~ Data: 2013-08-08,星期四
#~ Author:吳徐平
#~ Email:[email protected]
#~ #------------------------------------------------------------------
#~ #------------------------------------------------------------------
def IsSubString(SubStrList,Str):
#判斷字元串Str是否包含序列SubStrList中的每一個子字元串
#SubStrList=[F,EMS,txt]
#Str=F06925EMS91.txt
#IsSubString(SubStrList,Str)#return True (or False)
flag=True
for substr in SubStrList:
if not(substr in Str):
flag=False
return flag
#~ #----------------------------------------------------------------------
def GetFileList(FindPath,FlagStr=[]):
#獲取目錄中指定的文件名
#FlagStr=[F,EMS,txt] #要求文件名稱中包含這些字元
#FileList=GetFileList(FindPath,FlagStr) #
import os
FileList=[]
FileNames=os.listdir(FindPath)
if (len(FileNames)0):
for fn in FileNames:
if (len(FlagStr)0):
#返回指定類型的文件名
if (IsSubString(FlagStr,fn)):
fullfilename=os.path.join(FindPath,fn)
FileList.append(fullfilename)
else:
#默認直接返回所有文件名
fullfilename=os.path.join(FindPath,fn)
FileList.append(fullfilename)
#對文件名排序
if (len(FileList)0):
FileList.sort()
return FileList
可以使用pip在線安裝wlab
?
1
pip install wlab
還是給個圖吧:
希望本文所述對大家的Python程序設計有所幫助。

Ⅲ 如何用python獲取某一文件下所有函數名

import test
help(test)

Ⅳ python能不能動態獲取運行的文件名

不清楚你的問題是什麼?是指在程序當中獲取本程序的文件名稱嗎?可以使用sys.argv來傳遞這個參數。

例如,建立一個Python代碼文件,起名為self_known.py。內含以下內容:

importsys
printsys.argv[0]

運行這個文件:

pythonself_known.py

可以發現運行結果就是文件名稱:

self_known.py


在運行Python代碼時,不僅可以送入「文件名稱」一個參數,還可以送入更多參數:

pythonself_known.pyarg1arg2arg3

此時,在代碼文件中可以這樣得到這些參數:

printsys.argv[0]
printsys.argv[1]
printsys.argv[2]
printsys.argv[3]

Ⅳ python如何獲取函數的參數名

我這里用的是IDLE(我自己也覺得有點低端),Python3(2應該也可以)
>>> help()

Welcome to Python 3.7's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.7/tutorial/.

Enter the name of any mole, keyword, or topic to get help on writing
Python programs and using Python moles. To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available moles, keywords, symbols, or topics, type
"moles", "keywords", "指型旁symbols", or "topics". Each mole also comes
with a one-line summary of what it does; to list the moles whose name
or summary contain a given string such as "spam", type "moles spam".

help> sum
Help on built-in function sum in mole builtins:

sum(iterable, start=0, /)
Return the sum of a 'start' value (default: 0) plus an iterable of numbers

When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and may
reject non-numeric types.
解釋一下:先在Shell輸入help(),它就會問你你要哪個租辯函數的說明。然後你輸入對應函數(比如sum),就可以看到這一行:sum(iterable, start=0, /),也就是說你要先輸入iterable參數,start可以選擇輸入(有默認值)。

或唯橡者還有一種方法:用的時候直接輸入函數加上左括弧 比如sum( 然後你就可以看到下面有一個框,然後按照說明寫就好了。如果不小心不見了,就可以把左括弧去掉再重新輸入,就可以再看到這個框啦!

Ⅵ python如何獲取不含路徑的當前文件夾的名稱(不含子目錄)

import os
print(os.getcwd())
print(os.getcwd().split('\')[-1])

閱讀全文

與python獲取當前方法名相關的資料

熱點內容
膨脹加強帶梁箍筋要不要加密 瀏覽:667
排序演算法大全及時間復雜度 瀏覽:341
壓縮文件時報錯 瀏覽:611
圖的演算法有什麼用 瀏覽:793
櫃式空調壓縮機在哪裡 瀏覽:724
創建多級文件夾視頻講解 瀏覽:195
計算機有很多空文件夾能刪嗎 瀏覽:486
android44w下載 瀏覽:248
測排卵app哪個好用 瀏覽:622
平時解壓方法圖解 瀏覽:56
php多長時間能學會 瀏覽:308
apex伺服器選什麼好 瀏覽:595
nds編譯 瀏覽:110
各個擊破法動態規劃法貪婪演算法 瀏覽:533
柱端部為什麼要加密 瀏覽:891
c單鏈表排序演算法 瀏覽:362
python鄰接鏈表 瀏覽:369
外國解壓的視頻說話助眠 瀏覽:115
查看php擴展版本 瀏覽:239
社保卡加密機放在信息中心 瀏覽:95