대학 프로젝트 용으로 자체 수정 코드를 사용하고 있습니다.Python - IDLE을 사용하는 Python 2.5.6에서 코드 스 니펫이 작동하지 않습니다.
는 여기있다 :
import datetime
import inspect
import re
import sys
def main():
# print the time it is last run
lastrun = 'Mon Jun 8 16:31:27 2009'
print "This program was last run at ",
print lastrun
# read in the source code of itself
srcfile = inspect.getsourcefile(sys.modules[__name__])
f = open(srcfile, 'r')
src = f.read()
f.close()
# modify the embedded timestamp
timestamp = datetime.datetime.ctime(datetime.datetime.now())
match = re.search("lastrun = '(.*)'", src)
if match:
src = src[:match.start(1)] + timestamp + src[match.end(1):]
# write the source code back
f = open(srcfile, 'w')
f.write(src)
f.close()
if __name__=='__main__':
main()
불행하게도, 그것은 작동하지 않습니다. 오류가 반환되었습니다 :
# This is the script's output
This program is last run at Mon Jun 8 16:31:27 2009
# This is the error message
Traceback (most recent call last):
File "C:\Users\Rui Gomes\Desktop\teste.py", line 30, in <module>
main()
File "C:\Users\Rui Gomes\Desktop\teste.py", line 13, in main
srcfile = inspect.getsourcefile(sys.modules[__name__])
File "C:\Python31\lib\inspect.py", line 439, in getsourcefile
filename = getfile(object)
File "C:\Python31\lib\inspect.py", line 401, in getfile
raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module '__main__' (built-in)> is a built-in module
모든 해결책에 대해 감사드립니다.
자체 수정 코드는 악의적 인 코드입니다. 어떤 언어로. –
IDLE 2.5.6에서 \ Python31 \ lib 리소스를 호출하고 있습니다. 경로를 확인 하시겠습니까? – Don
와우, 그건 나쁜 생각이야. 실행 시간을 다른 곳에서 기록 할 수없는 이유는 무엇입니까? –