最明显的方法是打印到文件对象:
with open('out.txt', 'w') as f: print >> f, 'Filename:', filename # Python 2.x print('Filename:', filename, file=f) # Python 3.x但是,重定向标准输出对我也有效。像这样的一次性脚本可能很好:
import sysorig_stdout = sys.stdoutf = open('out.txt', 'w')sys.stdout = ffor i in range(2): print 'i = ', isys.stdout = orig_stdoutf.close()从外壳本身进行外部重定向是另一个不错的选择:
./script.py > out.txt
其他问题:
脚本中的第一个文件名是什么?我看不到它已初始化。
我的第一个猜测是glob找不到任何bamfile,因此for循环不会运行。检查文件夹是否存在,并在脚本中打印出bamfiles。



