256

我想使用 Python 将打印重定向到 .txt 文件。我有一个循环,当我想将所有输出重定向到一个文件时for,它将print输出我的每个 .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)
4

14 回答 14

379

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

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

但是,重定向标准输出也适用于我。像这样的一次性脚本可能很好:

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')

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

./script.py > out.txt

其他问题:

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

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

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

于 2011-08-22T20:00:19.063 回答
96

您可以使用参数重定向打印file(在 Python 2 中有>>操作符)。

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')))
于 2011-08-22T19:56:33.870 回答
48

Python 2Python 3 API 参考:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

文件参数必须是带有方法的对象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)
于 2016-07-04T13:45:25.713 回答
39

这完美地工作:

import sys
sys.stdout=open("test.txt","w")
print ("hello")
sys.stdout.close()

现在 hello 将被写入 test.txt 文件。确保stdout用 a关闭close,没有它,内容将不会保存在文件中

于 2015-06-30T12:15:25.490 回答
35

不使用print,使用logging

您可以更改sys.stdout为指向文件,但这是处理此问题的一种非常笨拙且不灵活的方法。而不是使用print,使用logging模块。

使用logging,您可以像您想要的那样打印stdout,或者您也可以将输出写入文件。您甚至可以使用不同的消息级别(critical, error, warning, info, debug),例如,仅将主要问题打印到控制台,但仍将次要代码操作记录到文件中。

一个简单的例子

导入logging、获取logger、设置处理级别:

import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG) # process everything, even if everything isn't printed

如果要打印到标准输出:

ch = logging.StreamHandler()
ch.setLevel(logging.INFO) # or any other level
logger.addHandler(ch)

如果您还想写入文件(如果您只想写入文件,请跳过最后一部分):

fh = logging.FileHandler('myLog.log')
fh.setLevel(logging.DEBUG) # or any level you want
logger.addHandler(fh)

然后,无论您在哪里使用,都可以print使用以下logger方法之一:

# print(foo)
logger.debug(foo)

# print('finishing processing')
logger.info('finishing processing')

# print('Something may be wrong')
logger.warning('Something may be wrong')

# print('Something is going really bad')
logger.error('Something is going really bad')

要了解有关使用更高级功能的更多信息,请阅读Python 文档中logging的优秀教程。logging

于 2018-01-01T20:54:59.340 回答
13

最简单的解决方案不是通过 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 (使脚本对所有用户都可执行)

于 2011-08-22T20:24:09.613 回答
10

如果您使用的是 Linux,我建议您使用该tee命令。实现是这样的:

python python_file.py | tee any_file_name.txt

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

于 2018-01-23T15:51:35.177 回答
5

您可能不喜欢这个答案,但我认为这是正确的答案。除非绝对必要,否则不要更改您的 stdout 目标(也许您正在使用仅输出到 stdout 的库???显然不是这里的情况)。

我认为作为一个好习惯,您应该提前将数据准备为字符串,然后打开文件并立即编写整个内容。这是因为输入/输出操作打开文件句柄的时间越长,该文件发生错误的可能性就越大(文件锁定错误、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()
于 2011-08-22T20:20:07.950 回答
3

如果重定向stdout对您的问题有效,Gringo Suave 的回答是如何做到这一点的一个很好的示范。

为了使它更容易,我使用上下文管理器制作了一个版本,使用以下语句获得简洁的通用调用语法with

from contextlib import contextmanager
import sys

@contextmanager
def redirected_stdout(outstream):
    orig_stdout = sys.stdout
    try:
        sys.stdout = outstream
        yield
    finally:
        sys.stdout = orig_stdout

要使用它,您只需执行以下操作(源自 Suave 的示例):

with open('out.txt', 'w') as outfile:
    with redirected_stdout(outfile):
        for i in range(2):
            print('i =', i)

print当模块以您不喜欢的方式使用它时,它对于有选择地重定向很有用。唯一的缺点(这是许多情况下的交易破坏者)是,如果一个人想要多个具有不同值的线程,它就不起作用stdout,但这需要一种更好、更通用的方法:间接模块访问。您可以在此问题的其他答案中看到它的实现。

于 2019-01-06T04:48:39.813 回答
1

我可以使用以下方法破解它。它将使用此打印功能而不是内置打印功能并将内容保存到文件中。

from __future__ import print_function
import builtins as __builtin__

log = open("log.txt", "a")

def print(*args):
    newLine = ""
    for item in args:
        newLine = newLine + str(item) + " "
    newLine = (
        newLine
        + """
"""
    )
    log.write(newLine)
    log.flush()
    __builtin__.print(*args)
    return
于 2021-09-18T19:51:54.350 回答
0

更改 sys.stdout 的值确实会更改所有打印调用的目标。如果您使用其他方式更改打印目的地,您将获得相同的结果。

您的错误在其他地方:

  • 它可能在您为您的问题删除的代码中(文件名来自哪里来调用打开?)
  • 也可能是您没有等待刷新数据:如果您在终端上打印,则在每个新行之后刷新数据,但如果您打印到文件,则仅在 stdout 缓冲区已满时才刷新(4096 字节在大多数系统上)。
于 2011-08-22T20:05:14.863 回答
0

这是我用于打印到文件/日志的另一种方法...修改内置打印功能,使其使用当前时间戳记录到临时目录中的文件,并打印到标准输出。在脚本中这样做的唯一真正好处是不必去修改现有的打印语句。

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
于 2022-01-25T15:28:58.783 回答
0

在 python 3 中,您可以重新分配print

#!/usr/bin/python3

def other_fn():
    #This will use the print function that's active when the function is called
    print("Printing from function")

file_name = "test.txt"
with open(file_name, "w+") as f_out:
    py_print = print #Need to use this to restore builtin print later, and to not induce recursion
   
    print = lambda out_str : py_print(out_str, file=f_out)
    
    #If you'd like, for completeness, you can include args+kwargs
    print = lambda *args, **kwargs : py_print(*args, file=f_out, **kwargs)
    
    print("Writing to %s" %(file_name))

    other_fn()  #Writes to file

    #Must restore builtin print, or you'll get 'I/O operation on closed file'
    #If you attempt to print after this block
    print = py_print

print("Printing to stdout")
other_fn() #Writes to console/stdout

请注意,print fromother_fn仅切换输出,因为print正在全局范围内重新分配。如果我们在函数中分配print ,通常不会影响print in 。如果我们想影响所有的打印调用,other_fn我们可以使用global关键字:

import builtins

def other_fn():
    #This will use the print function that's active when the function is called
    print("Printing from function")

def main():
    global print #Without this, other_fn will use builtins.print
    file_name = "test.txt"
    with open(file_name, "w+") as f_out:

        print = lambda *args, **kwargs : builtins.print(*args, file=f_out, **kwargs)

        print("Writing to %s" %(file_name))

        other_fn()  #Writes to file

        #Must restore builtin print, or you'll get 'I/O operation on closed file'
        #If you attempt to print after this block
        print = builtins.print

    print("Printing to stdout")
    other_fn() #Writes to console/stdout

就个人而言,我更喜欢print通过将输出文件描述符烘焙到一个新函数中来回避使用该函数的要求:

file_name = "myoutput.txt"
with open(file_name, "w+") as outfile:
    fprint = lambda pstring : print(pstring, file=outfile)
    print("Writing to stdout")
    fprint("Writing to %s" % (file_name))
于 2020-12-08T19:04:27.567 回答
-1

为循环扩展打印功能的东西

x = 0
while x <=5:
    x = x + 1
    with open('outputEis.txt', 'a') as f:
        print(x, file=f)
    f.close()
于 2017-07-16T19:34:04.957 回答