我有几个包含脚本的数据文件夹ExportScriptGen.py
,我想要一个可以遍历目录结构并ExportScriptGen.py
在找到它的每个文件夹中运行的脚本。这是我到目前为止所拥有的:
import sys
import os
import os.path
import subprocess
script = "ExportScriptGen.py"
if len(sys.argv) > 1:
top = sys.argv[1]
else:
top = os.path.realpath(".")
for root, dirs, files in os.walk(top):
if (script in files):
subprocess.call(os.path.join(root,script), shell=True)
ExportScriptGen.py
在另一个文件夹中生成脚本(并经过测试)。当我小规模测试我的主脚本时(即一个ExportScriptGen.py
没有子目录的文件夹),一切正常。但是,当我在我的全面问题(有大量目录/子目录)上运行它时,不会产生任何输出ExportScriptGen.py
。为什么会这样?我在 Windows 7 上运行 Python 3.2。
这是ExportScriptGen.py
:
import sys
import os.path
import os
### Declarations ###
script_check_dir = # directory to place scripts in
layout_name = # parameter to write into top of script
template_name = # template for script
out_script_tail = # string to add at end of output script file
### Code ###
if len(sys.argv) > 1:
folder = sys.argv[1]
else:
folder = os.path.dirname(os.path.realpath(__file__))
data_files = filter(os.path.isfile, os.listdir(folder))
data_files = [f for f in data_files if ("." in f) and os.path.splitext(f)[1][1].isdigit()]
for dfile in data_files:
out_script_name = os.path.join(script_check_dir, dfile+out_script_tail)
out_script = open(out_script_name, 'w')
out_script.write("folder = \"{}\\\"\n".format(folder))
out_script.write("layout = folder + \"{}\"\n".format(layout_name))
out_script.write("dataFile = folder + \"{}\"\n".format(dfile))
template = open(os.path.join(folder, template_name), 'r')
for line in template:
out_script.write(line)
out_script.close()