我想使用Python将打印重定向到一个.txt文件。我有一个for循环,它将打印每个.bam文件的输出,而我想将所有输出重定向到一个文件。所以我试着说:

f = open('output.txt','w')
sys.stdout = f

在我剧本的开头。但是,我在.txt文件中什么也没有得到。 我的剧本是:

#!/usr/bin/python

import os,sys
import subprocess
import glob
from os import path

f = open('output.txt','w')
sys.stdout = f

path= '/home/xxx/nearline/bamfiles'
bamfiles = glob.glob(path + '/*.bam')

for bamfile in bamfiles:
    filename = bamfile.split('/')[-1]
    print 'Filename:', filename
    samtoolsin = subprocess.Popen(["/share/bin/samtools/samtools","view",bamfile],
                                  stdout=subprocess.PIPE,bufsize=1)
    linelist= samtoolsin.stdout.readlines()
    print 'Readlines finished!'

那么问题是什么呢?除了sys。stdout还有其他方法吗?

我需要我的结果看起来像:

Filename: ERR001268.bam
Readlines finished!
Mean: 233
SD: 10
Interval is: (213, 252)

当前回答

你可以用file参数重定向打印(在python2中有>>操作符代替)。

f = open(filename,'w')
print('whatever', file=f) # Python 3.x
print >>f, 'whatever'     # Python 2.x

在大多数情况下,最好只是正常地写入文件。

f.write('whatever')

或者,如果你有几个项目想用空格写,比如print:

f.write(' '.join(('whatever', str(var2), 'etc')))

其他回答

你可以用file参数重定向打印(在python2中有>>操作符代替)。

f = open(filename,'w')
print('whatever', file=f) # Python 3.x
print >>f, 'whatever'     # Python 2.x

在大多数情况下,最好只是正常地写入文件。

f.write('whatever')

或者,如果你有几个项目想用空格写,比如print:

f.write(' '.join(('whatever', str(var2), 'etc')))

Python 2或Python 3 API参考:

Print (*objects, sep=' ', end='\n', file=sys. txt)stdout,冲洗= False) file参数必须是一个具有write(string)方法的对象;如果不存在或为None,则sys。将使用Stdout。由于打印参数被转换为文本字符串,print()不能用于二进制模式的文件对象。对于这些,使用file.write(…)代替。

由于文件对象通常包含write()方法,您所需要做的就是将一个文件对象传递到它的参数中。

写入/覆盖到文件

with open('file.txt', 'w') as f:
    print('hello world', file=f)

写入/追加到文件

with open('file.txt', 'a') as f:
    print('hello world', file=f)

最明显的方法是打印到一个文件对象:

with open('out.txt', 'w') as f:
    print('Filename:', filename, file=f)  # Python 3.x
    print >> f, 'Filename:', filename     # Python 2.x

不过,重定向stdout对我也有用。对于像这样的一次性脚本,它可能很好:

import sys

orig_stdout = sys.stdout
f = open('out.txt', 'w')
sys.stdout = f

for i in range(2):
    print('i = ', i)

sys.stdout = orig_stdout
f.close()

从Python 3.4开始,就有一个简单的上下文管理器可以在标准库中做到这一点:

from contextlib import redirect_stdout

with open('out.txt', 'w') as f:
    with redirect_stdout(f):
        print('data')

从shell本身向外部重定向是另一种选择,通常是更可取的:

./script.py > out.txt

其他问题:

你的脚本的第一个文件名是什么?我没有看到它被初始化。

我的第一个猜测是glob没有找到任何bamfile,因此for循环不会运行。检查文件夹是否存在,并在脚本中打印bamfiles。

同样,使用os.path.join和os.path.basename来操作路径和文件名。

修改sys. exe的值。Stdout将所有调用的目的地更改为打印。如果您使用另一种方法来更改打印的目的地,则会得到相同的结果。

你的bug在别的地方:

它可能在你为你的问题删除的代码中(filename从哪里来的调用打开?) 也可能是您没有等待数据被刷新:如果您在终端上打印,则在每换行后都会刷新数据,但如果您打印到文件中,则仅在stdout缓冲区满时才会刷新数据(在大多数系统上为4096字节)。

如果您正在使用Linux,我建议您使用tee命令。实现如下所示:

python python_file.py | tee any_file_name.txt

如果您不想更改代码中的任何内容,我认为这可能是最好的解决方案。您也可以实现记录器,但您需要在代码中做一些更改。