我想使用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)

当前回答

最简单的解决方案不是通过python;它穿过外壳。从文件的第一行(#!/usr/bin/python),我猜您是在UNIX系统上。只需像平时一样使用print语句,在脚本中完全不要打开文件。当你运行文件时,而不是

./script.py

要运行该文件,请使用

./script.py > <filename>

您将<filename>替换为您希望输出进入的文件的名称。>令牌告诉(大多数)shell将stdout设置为以下令牌所描述的文件。

这里需要提到的一件重要的事情是,“script.py”需要被设置为可执行的,以便。/script.py能够运行。

所以在运行。/script.py之前,执行这个命令

Chmod a+x script.py (使脚本对所有用户都可执行)

其他回答

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)

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

你的bug在别的地方:

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

这是我用来打印到文件/日志的另一种方法…修改内置的打印函数,使其记录到具有当前时间戳的temp目录中的文件,并打印到stdout。在脚本中这样做的唯一真正好处是不需要去修改现有的打印语句。

print('test')
test

复制原始打印函数到新变量

og_print = print
og_print('test2')
test2

覆盖现有的打印功能

def print(*msg):
    '''print and log!'''
    # import datetime for timestamps
    import datetime as dt
    # convert input arguments to strings for concatenation
    message = []
    for m in msg:
        message.append(str(m))
    message = ' '.join(message)
    # append to the log file
    with open('/tmp/test.log','a') as log:
        log.write(f'{dt.datetime.now()} | {message}\n')
    # print the message using the copy of the original print function to stdout
    og_print(message)
print('test3')
test3

显示文件

cat /tmp/test.log
2022-01-25 10:19:11.045062 | test3

删除文件

rm /tmp/test.log

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

python python_file.py | tee any_file_name.txt

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

你可能不喜欢这个答案,但我认为这是正确的答案。除非绝对必要,否则不要改变你的标准输出目标(也许你使用的库只输出到标准输出??这里显然不是这样)。

我认为作为一个好习惯,你应该提前准备好你的数据作为一个字符串,然后打开你的文件,并立即写入整个东西。这是因为输入/输出操作打开文件句柄的时间越长,该文件发生错误的可能性就越大(文件锁定错误、i/o错误等)。只要在一次操作中完成所有操作,就不会有什么时候可能出错的问题。

这里有一个例子:

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

然后,当你收集了你的“数据线”,每个列表项一行,你可以用一些'\n'字符连接它们,使整个东西可输出;甚至可以将输出语句包装在with块中,以获得额外的安全性(即使出现错误,也会自动关闭输出句柄):

out_string = '\n'.join(out_lines)
out_filename = 'myfile.txt'
with open(out_filename, 'w') as outf:
    outf.write(out_string)
print "YAY MY STDOUT IS UNTAINTED!!!"

然而,如果你有很多数据要写,你可以一次写一块。我认为这与你的应用程序无关,但这里有另一种选择:

out_filename = 'myfile.txt'
outf = open(out_filename, 'w')
for bamfile in bamfiles:
    filename = bamfile.split('/')[-1]
    outf.write('Filename: %s' % filename)
    samtoolsin = subprocess.Popen(["/share/bin/samtools/samtools","view",bamfile],
                                  stdout=subprocess.PIPE,bufsize=1)
    mydata = samtoolsin.stdout.read()
    outf.write(mydata)
outf.close()