我想創建python腳本,可以使用Python Language Services或使用任何其他方式修改該腳本本身的代碼。
例如它保持它的全成執行自我修改python腳本
import re
COUNT = 0
def updateCount():
# code to update second line e.g. COUNT = 0
pass
if __name__ == '__main__':
print('This script has run {} times'.format(COUNT))
updateCount()
的計數的軌道上的這個腳本代碼成功執行的腳本應該得到改變
import re
COUNT = 1
def updateCount():
# code to update second line e.g. COUNT = 0
pass
if __name__ == '__main__':
print('This script has run {} times'.format(COUNT))
updateCount()
簡單的方式來到我的腦海裏是寫模式打開__file__
和做requried修改使用reguler expessions等,但沒有工作,我得到了異常io.UnsupportedOperation: not readable
。即使這種方法可行,那也是非常危險的,因爲它會破壞我的整個腳本。所以我正在尋找使用Python語言服務的解決方案。
您可以打開並編輯'__file__'。 – kichik
請注意,您可能會遇到權限問題(如果腳本是由沒有寫入權限的用戶運行的話)。 – Leon
不起作用'io.UnsupportedOperation:不可讀' –