[本文出自天外歸雲的博客園]
在Auty的scripts文件夾中編寫一個create_selection.py文件,用於在同級目錄下針對同級目錄scripts下的所有腳本生成一個selection.txt文件,其中包含所有同級目錄scripts文件夾下可執行的python腳本:
代碼如下:
# -*- coding: utf-8 -*- import sys import os def create_selection(): path = sys.path[0] selection = [] for i in os.walk(os.path.join(path,'scripts')): for fileName in i[2:3][0]: filePath = os.path.join(i[0],fileName) if(check_if_python(filePath)): selection.append(filePath) return selection def check_if_python(fileName): if fileName.endswith('.py'): return True def create_selection_file(selection): filePath = os.path.join(sys.path[0],'all_scripts_selection.txt') theFile = open(filePath,'w') for scriptPath in selection: theFile.write(scriptPath+'\n') theFile.close() if __name__ == '__main__': selection = create_selection() create_selection_file(selection)
執行這個腳本,就會生成所有可以執行的腳本文件列表。