मैं पाइथन लिपि में फ़ाइल का नाम और लाइन नंबर कैसे प्राप्त कर सकता हूं।फ़ाइल नाम और पायथन लिपि की लाइन संख्या
सटीक रूप से फ़ाइल जानकारी जो हम अपवाद ट्रेसबैक से प्राप्त करते हैं। इस मामले में अपवाद उठाए बिना।
मैं पाइथन लिपि में फ़ाइल का नाम और लाइन नंबर कैसे प्राप्त कर सकता हूं।फ़ाइल नाम और पायथन लिपि की लाइन संख्या
सटीक रूप से फ़ाइल जानकारी जो हम अपवाद ट्रेसबैक से प्राप्त करते हैं। इस मामले में अपवाद उठाए बिना।
inspect.currentframe().f_back.f_lineno
ActiveState Code से।
यह गलत है और इसके बजाय संकलित FOO.pyc फ़ाइल की लाइन संख्या में परिणाम होता है। यहां मैं 'संदेश =' कोड स्थान {0.filename} @ {0.lineno}: 'प्रारूप (निरीक्षण .getframeinfo (inspect.currentframe())) के बजाय उपयोग करता हूं, जो @ arilou की पोस्ट के अनुसार है। –
यह फाइलें –
फ़ाइलों को शामिल करते समय ठीक से काम नहीं करता है: यह सभी सही प्रश्न नहीं है। ** 15 से अधिक वोटों के साथ, सही उत्तर ** नीचे है। –
धन्यवाद mcandre करने के लिए, जवाब है:
from inspect import currentframe, getframeinfo
frameinfo = getframeinfo(currentframe())
print frameinfo.filename, frameinfo.lineno
इस विधि का उपयोग करने से कोई प्रदर्शन प्रभाव पड़ता है (जैसे रन में मामूली वृद्धि समय या अधिक सीपीयू की आवश्यकता है)? – gsinha
@gsinha: प्रत्येक फ़ंक्शन कॉल का प्रदर्शन प्रभाव पड़ता है। अगर आपको यह प्रभाव आपके लिए स्वीकार्य है तो आपको मापना होगा। – omikron
तो, यदि आपको "एक पंक्ति" प्रकार का उत्तर उपयोग पसंद है: 'आयात निरीक्षण निरीक्षण .getframeinfo (inspect.currentframe())। Lineno' –
फ़ाइल का नाम: __file__
या sys.argv[0]
पंक्ति: inspect.currentframe().f_lineno
(नहीं inspect.currentframe() जैसा कि ऊपर उल्लेख f_back.f_lineno।)
' नाम त्रुटि: वैश्विक पायदान '__file__' को मेरे पायथन दुभाषिया पर परिभाषित नहीं किया गया है: 'पायथन 2.7.6 (डिफ़ॉल्ट, 26 सितंबर 2014, 15:59:23)'। एक कॉल करने योग्य फ़ंक्शन में समाधान प्रदान करने के लिए http://stackoverflow.com/questions/9271464/what-does-the-file-variable-mean-do/9271617#comment33090606_9271479 – bgoodr
आप चाहे currentframe().f_back
का उपयोग इस बात पर निर्भर करता है कि आप फ़ंक्शन का उपयोग कर रहे हैं या नहीं।
कॉलिंग सीधे निरीक्षण:
from inspect import currentframe, getframeinfo
cf = currentframe()
filename = getframeinfo(cf).filename
print "This is line 5, python says line ", cf.f_lineno
print "The filename is ", filename
एक समारोह है कि यह आप के लिए करता है कॉलिंग:
from inspect import currentframe
def get_linenumber():
cf = currentframe()
return cf.f_back.f_lineno
print "This is line 7, python says line ", get_linenumber()
प्लस वन देखें। बहुत अच्छा! – MikeyE
import inspect
file_name = __FILE__
current_line_no = inspect.stack()[0][2]
current_function_name = inspect.stack()[0][3]
#Try printing inspect.stack() you can see current stack and pick whatever you want
'__file__' के समान: देखें http://stackoverflow.com/questions/3056048/filename-and-line-number-of-python-script#comment74661035_10623150 – bgoodr
बस योगदान करने के लिए,
वहाँ अजगर में एक linecache
मॉड्यूल है, यहां दो लिंक हैं जो मदद कर सकते हैं।
linecache module documentation
linecache source code
एक मायने में, आप अपने कैश में एक पूरे फ़ाइल "डंप" कर सकते हैं और वर्ग से linecache.cache डेटा के साथ पढ़ें।
import linecache as allLines
## have in mind that fileName in linecache behaves as any other open statement, you will need a path to a file if file is not in the same directory as script
linesList = allLines.updatechache(fileName ,None)
for i,x in enumerate(lineslist): print(i,x) #prints the line number and content
#or for more info
print(line.cache)
#or you need a specific line
specLine = allLines.getline(fileName,numbOfLine)
#returns a textual line from that number of line
अतिरिक्त जानकारी के लिए, त्रुटि से निपटने के लिए, आप बस का उपयोग कर सकते
from sys import exc_info
try:
raise YourError # or some other error
except Exception:
print(exc_info())
हैंडी अगर किसी सामान्य फ़ाइल में इस्तेमाल किया - प्रिंट नाम, लाइन नंबर और समारोह फोन करने वाले की फाइल:
import inspect
def getLineInfo():
print(inspect.stack()[1][1],":",inspect.stack()[1][2],":",
inspect.stack()[1][3])
बेहतर
print dir(sys._getframe())
print dir(sys._getframe().f_lineno)
print sys._getframe().f_lineno
also- sys उपयोग करने के लिए
आउटपुट है:
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'f_back', 'f_builtins', 'f_code', 'f_exc_traceback', 'f_exc_type', 'f_exc_value', 'f_globals', 'f_lasti', 'f_lineno', 'f_locals', 'f_restricted', 'f_trace']
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
14
मैं आपको कम कर रहा हूं ताकि आप देख सकें कि आपका स्वीकृत उत्तर गलत है। – philologon
@philologon शुभकामनाएँ। जॉय को आखिरी बार 3 साल पहले देखा गया था। –
रुको, वह कैसे आता है कि उसने अपना जवाब स्वीकार नहीं किया ?! –