相关疑难解决方法(0)

在Python中使函数的stdout静音而不会破坏sys.stdout并恢复每个函数调用

有没有办法让Python在没有包含像下面这样的函数调用的情况下使stdout静音?

原始破码:

from sys import stdout
from copy import copy
save_stdout = copy(stdout)
stdout = open('trash','w')
foo()
stdout = save_stdout
Run Code Online (Sandbox Code Playgroud)

编辑:更正了Alex Martelli的代码

import sys
save_stdout = sys.stdout
sys.stdout = open('trash', 'w')
foo()
sys.stdout = save_stdout
Run Code Online (Sandbox Code Playgroud)

这种方式有效,但似乎非常低效.有是一个更好的办法.有任何想法吗?

python stdout

46
推荐指数
6
解决办法
4万
查看次数

当我使用脚本名称在Windows上运行Python脚本时,无法重定向输出

这是在Windows 7(64位),Python 2.6和Win32 Extensions for Python上运行的.

我有一个简单的脚本,只打印"你好世界".我可以用它启动它python hello.py.在这种情况下,我可以将输出重定向到文件.但是如果我只是hello.py在命令行输入并重定向输出来运行它,我会得到一个例外.

C:> python hello.py
hello world

C:> python hello.py >output

C:> type output
hello world

C:> hello.py
hello world

C:> hello.py >output
close failed in file object destructor:
Error in sys.excepthook:

Original exception was:
Run Code Online (Sandbox Code Playgroud)

我想我升级到Windows 7后首先得到此错误.我记得它应该在XP中运行.我见过有人在谈论这个bug python-Bugs-1012692 | 无法将输入传递给python程序.但那是很久以前的事了.它没有提到任何解决方案.

有没有人经历过这个?有人可以帮忙吗?

python winapi redirect command-line windows-7

21
推荐指数
2
解决办法
2万
查看次数

即使将 `verbose` 设置为 `False`,Moviepy 仍然会打印进度条

我试图在调用“write_videofile”方法时抑制从 moviepy 产生的控制台输出。我将冗长的参数传递为 False 无济于事。它仍然输出如下内容:

0%| | 0/1624 [00:00<?, ?it/s]
0%| | 8/1624 [00:00<00:20, 77.64it/s]
1%| | 16/1624 [00:00<00:20, 78.31it/s]
2%|1 | 25/1624 [00:00<00:20, 77.90it/s]
2%|2 | 34/1624 [00:00<00:19, 80.80it/s]
3%|2 | 42/1624 [00:00<00:20, 75.91it/s]
3%|3 | 51/1624 [00:00<00:20, 76.07it/s]
4%|3 | 58/1624 [00:00<00:25, 62.44it/s]
4%|4 | 65/1624 [00:00<00:28, 54.77it/s]
4%|4 | 71/1624 [00:01<00:28, 53.63it/s]
5%|4 | 77/1624 [00:01<00:29, 52.69it/s]
5%|5 | 83/1624 [00:01<00:28, 54.06it/s]
5%|5 | 89/1624 [00:01<00:29, 52.80it/s]
6%|5 | 96/1624 [00:01<00:26, 56.95it/s]
6%|6 | 102/1624 [00:01<00:29, 52.38it/s] …
Run Code Online (Sandbox Code Playgroud)

python moviepy

2
推荐指数
2
解决办法
4451
查看次数

标签 统计

python ×3

command-line ×1

moviepy ×1

redirect ×1

stdout ×1

winapi ×1

windows-7 ×1