相关疑难解决方法(0)

sys.stdout.write和print之间的区别?

有哪些sys.stdout.write()情况比较好print

(示例:更好的性能;更有意义的代码)

python printing stdout

351
推荐指数
9
解决办法
54万
查看次数

print()vs sys.stdout.write():哪个和为什么?

我有一个用sys.stdout.write()和创建输出的脚本sys.stderr.write().最近我发布了一些关于脚本其他方面的问题,我注意到发布答案的人似乎更喜欢print('some text', file=sys.stdout).我对输出的主要关注只是它可以被shell传送到文件或作为另一个程序的输入.有人可以解释(或指导我现有的解释)两者之间的差异,print()以及sys.stdout.write每个应该使用的情况和这些惯例的合理性吗?

python python-3.x

30
推荐指数
1
解决办法
5万
查看次数

如何使用非阻塞IO写入文件?

我想在Python中使用非阻塞方法写入文件.在一些谷歌上,我发现语言支持fcntl这样做,但实现相同的方法对我来说不是很清楚.

这是代码片段(我不知道我哪里出错了):

import os, fcntl
nf = fcntl.fcntl(0,fcntl.F_UNCLK)
fcntl.fcntl(0,fcntl.F_SETFL , nf | os.O_NONBLOCK )
nf = open ("test.txt", 'a') 
nf.write ( " sample text \n")
Run Code Online (Sandbox Code Playgroud)

这是对文件执行非阻塞IO操作的正确方法吗?我对此表示怀疑.另外,你能否在Python中建议任何其他允许我这样做的模块?

python file-io nonblocking

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

标签 统计

python ×3

file-io ×1

nonblocking ×1

printing ×1

python-3.x ×1

stdout ×1