導航:首頁 > 編程語言 > 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獲取當前方法名相關的資料

熱點內容
程序員有天賦 瀏覽:640
編程計算並輸出半徑 瀏覽:709
日期排序java 瀏覽:952
程序員的中介模式 瀏覽:880
python中求兩個字元串重復部分 瀏覽:494
如何查看收件發件伺服器 瀏覽:110
linux配置android環境變數 瀏覽:132
android文字測量 瀏覽:883
壓力大金錢怎麼解壓 瀏覽:641
生男演算法 瀏覽:770
php讀取當前目錄 瀏覽:439
說一個可以連接的伺服器地址 瀏覽:508
操盤手1pdf 瀏覽:536
什麼app新人注冊可以免費買東西 瀏覽:601
flexjava教程 瀏覽:119
vnc訪問雲伺服器 瀏覽:484
java獲取list長度 瀏覽:693
linuxc打開文件夾 瀏覽:891
分析員設計員程序員職責 瀏覽:711
如何修改安卓內購游戲 瀏覽:439