如何在python中将特定的print语句重定向到文本文件
我使用下面的语句将python程序的输出重定向到一个log_文件,它可以正常工作如何在python中将特定的print语句重定向到文本文件,python,Python,我使用下面的语句将python程序的输出重定向到一个log_文件,它可以正常工作 import sys sys.stdout = open(log_file, 'a', buffering=1) 但是,我想将一个或两个特定的打印语句传递到另一个平面文件(假设sample_file.txt)。我该怎么做 假设您想在现有文本文件中添加新行,您可以这样修改代码,非常简单: import sys sys.stdout = open(log_file, 'a', buffering=1) with op
import sys
sys.stdout = open(log_file, 'a', buffering=1)
但是,我想将一个或两个特定的打印语句传递到另一个平面文件(假设sample_file.txt)。我该怎么做 假设您想在现有文本文件中添加新行,您可以这样修改代码,非常简单:
import sys
sys.stdout = open(log_file, 'a', buffering=1)
with open("sample_file.txt", "a") as f:
f.write("line 1\n")
f.write("line 2\n")
请注意,文件
“sample_file.txt”
将在运行Python文件的任何文件夹/目录中进行编辑,除非您将其更改为指定完整(或相对)路径。假设您希望在现有文本文件中添加新行,您可以修改代码,非常简单:
import sys
sys.stdout = open(log_file, 'a', buffering=1)
with open("sample_file.txt", "a") as f:
f.write("line 1\n")
f.write("line 2\n")
请注意,文件
“sample_file.txt”
将在运行Python文件的任何文件夹/目录中进行编辑,除非您将其更改为指定完整(或相对)路径。如果您使用的是python3,您可以选择将fileobject传递给打印函数。像这样:
>>> f = open('sample_file.txt', 'w')
>>> print("hello", file = f)
>>> print("hello1", file = f)
>>> print("hello2", file = f)
>>> f.close()
或者,更好的方法是使用上下文管理器
>>> with open('etc.txt','w') as f:
... print("hello", file = f)
... print("hello2", file = f)
... print("hello2", file = f)
在python2中,您也可以使用此功能。但是您需要从python3导入print()
函数。添加此导入语句
from __future__ import print_function
如果您使用的是python3,您可以选择将fileobject传递给print函数。像这样:
>>> f = open('sample_file.txt', 'w')
>>> print("hello", file = f)
>>> print("hello1", file = f)
>>> print("hello2", file = f)
>>> f.close()
或者,更好的方法是使用上下文管理器
>>> with open('etc.txt','w') as f:
... print("hello", file = f)
... print("hello2", file = f)
... print("hello2", file = f)
在python2中,您也可以使用此功能。但是您需要从python3导入print()
函数。添加此导入语句
from __future__ import print_function
优雅的解决方案是以以下方式使用上下文管理器
print("hello") # Prints to the console
with log_to_file(sys, LOG_FILE_PATH):
print("hello") # Prints to the file
print("hello") # Prints to the console
要构建此上下文管理器,将使用以下代码
from contextlib import contextmanager
import sys
@contextmanager
def log_to_file(sys, LOG_FILE_PATH):
log_file = open(LOG_FILE_PATH, 'a')
sys.stdout = log_file
yield
log_file.close()
sys.stdout = sys.__stdout__
现在,您可以通过传递不同的文件路径来决定每次在何处写入代码,代码将变得干净,Pythonic优雅的解决方案将以以下方式使用上下文管理器
print("hello") # Prints to the console
with log_to_file(sys, LOG_FILE_PATH):
print("hello") # Prints to the file
print("hello") # Prints to the console
要构建此上下文管理器,将使用以下代码
from contextlib import contextmanager
import sys
@contextmanager
def log_to_file(sys, LOG_FILE_PATH):
log_file = open(LOG_FILE_PATH, 'a')
sys.stdout = log_file
yield
log_file.close()
sys.stdout = sys.__stdout__
现在,您可以通过传递不同的文件路径来决定每次写入的位置,并且您的代码将是干净的Python代码。在较旧的Python上,您可以使用的“V形打印”形式将特定的打印语句打印到文件:
Python 2.7.13 (default, Dec 19 2016, 07:22:39)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> f=open('/tmp/file.txt','a')
>>> print "hello"
hello
>>> print >>f, 'hello'
>>> f.close()
>>> open('/tmp/file.txt').read()
'hello\n'
表单print>>fh
打印文件句柄fh
或者在Python 2中导入print函数,以便能够向打印添加文件句柄。在较旧的Python上,可以使用的“V形打印”形式将特定的打印语句打印到文件:
Python 2.7.13 (default, Dec 19 2016, 07:22:39)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> f=open('/tmp/file.txt','a')
>>> print "hello"
hello
>>> print >>f, 'hello'
>>> f.close()
>>> open('/tmp/file.txt').read()
'hello\n'
表单print>>fh
打印文件句柄fh
或者在Python 2中导入print函数,以获得向打印添加文件句柄的功能。您可以在
附加模式下打开文件,并在其中写入数据:使用open(“sample_file.txt”,“a”)作为f:f.write(“hello world!\n”)
您可能想在project.python2或python3中使用日志记录
模块?我使用的是Python 2.6.6。您可以在附加模式下打开文件
并在其中写入数据:以open(“sample_file.txt”,“a”)作为f:f.write(“hello world!\n”)
您可能希望在您的项目中使用日志记录
模块。python2或python3?我使用的是Python 2.6.6。@rawse-您使用的是哪一版本的Python?我已经在我的环境中运行了代码(不包括日志文件打印),它可以按预期工作。但是,我忘记在我的两行末尾添加一个新行字符,我已经纠正了这个错误。我在尝试相同的操作时遇到了语法错误sys.stdout=open(“/opt/test/test\u user/data/PLANNING\uu%s\u%s.etab%”(type,period,self.now),“a”,buffering=1),带有open(“/opt/test/test\u user/data/SUB\u PLANNING\u%s\u%s.etab%”(type,period,self.now),“a”)作为e:
syntaxer:invalid syntax为什么不使用print
而不是write
,特别是当问题说他们希望“将语句打印到另一个文件”@rawse-您使用的是哪个版本的python?我已经在我的环境中运行了代码(不包括日志文件打印),它可以按预期工作。但是,我忘记在我的两行末尾添加一个新行字符,我已经纠正了这个错误。我在尝试相同的操作时遇到了语法错误sys.stdout=open(“/opt/test/test\u user/data/PLANNING\uu%s\u%s.etab%”(type,period,self.now),“a”,buffering=1),带有open(“/opt/test/test\u user/data/SUB\u PLANNING\u%s\u%s.etab%”(type,period,self.now),“a”)作为e:
syntaxer:invalid syntax为什么不使用print
而不是write
,特别是当问题说他们希望“将语句打印到另一个文件”时,它会的。您必须从python3导入函数。只需在文件的导入中添加来自_ufuture _uuimport print_函数
的行,就可以了。您必须从python3导入函数。只需在文件的导入中添加来自_future _导入打印功能
的行。