我认为这应该有效。我想在创建的带有标签名称的文件上为每个标签获取此命令 "grep -l %s *.py" % 标签的输出。
import os
import sys
results_dir = '/home/ks/one/work/tn/format-description 15852/results'
converters_dir = '/home/ks/one/work/cvr'
export_dir = '/home/ks/one/work/epr'
all_dirs = {'cvr':cvrs_dir,
'epr':eprs_dir}
tags = [tag.strip() for tag in open('new file').readlines()]
for coe, directory in all_dirs.items(): # coe - type of our file
os.chdir(results_dir)
for tag in tags:
tag_file = open(coe + ' ' + tag, 'w')
sys.stdout = tag_file
os.chdir(directory)
os.system("grep -l %s *.py" % tag)
tag_file.close()
但是我在运行脚本时看到的所有内容 - 它都在我的控制台中输出。