2024-06-16 14:20:45 发布
网友
./some_program > first_file.something 2> second_file.else
在python中如何实现这一点?你知道吗
https://docs.python.org/3/library/sys.html
sys.stdout sys.stderr
confused about stdin, stdout and stderr?
例如,在python3中,只需导入cout和cerr的等价物,即sys.stdout和sys.stderr。你知道吗
cout
cerr
sys.stdout
sys.stderr
from sys import stdout, stderr print('to standard output', file=stdout) print('to standard error', file=stderr)
然后可以像平常一样使用bash重定向:
python program.py 1>output 2>errors
如果你愿意,你甚至可以给他们取你喜欢的名字。例如:
from sys import stdout as cout, stderr as cerr print('to standard output', file=cout) print('to standard error', file=cerr)
它不那么“Pythic”,但是如果它有助于你从C++经验中弥补差距,它可能会有帮助。你知道吗
如果你想从命令行重定向,它和C++中的方式差不多:
python file.py > first_file.txt 2> second_file.txt
通过编程,您可以使用sys.stdout和sys.stderr来实现这一点,方法是使用您选择的文件对这些文件进行monkey修补。你知道吗
https://docs.python.org/3/library/sys.html
confused about stdin, stdout and stderr?
例如,在python3中,只需导入
cout
和cerr
的等价物,即sys.stdout
和sys.stderr
。你知道吗然后可以像平常一样使用bash重定向:
如果你愿意,你甚至可以给他们取你喜欢的名字。例如:
它不那么“Pythic”,但是如果它有助于你从C++经验中弥补差距,它可能会有帮助。你知道吗
如果你想从命令行重定向,它和C++中的方式差不多:
通过编程,您可以使用
sys.stdout
和sys.stderr
来实现这一点,方法是使用您选择的文件对这些文件进行monkey修补。你知道吗相关问题 更多 >
编程相关推荐