Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

Python: significance of -u option?

I am noticed in some python code that -u is used to start the python interpreter. I looked at the man page for python but I could not get much out of it. Please give me some examples.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From python --help:

-u     : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x
         see man page for details on internal buffering relating to '-u'

The manpage states:

-u     Force  stdin,  stdout and stderr to be totally unbuffered.  On systems where it matters, also put stdin,
       stdout and stderr in binary mode.  Note that there is internal buffering  in  xreadlines(),  readlines()
       and  file-object  iterators  ("for  line in sys.stdin") which is not influenced by this option.  To work
       around this, you will want to use "sys.stdin.readline()" inside a "while 1:" loop.

Python opens the stdin, -out and -error streams in a buffered mode; it'll read or write in larger chunks, keeping data in memory until a threshold is reached. -u disables those buffers.

Also, python can interpret newlines on open files and translate them from and to the native platform newlines (text mode). The -u option disables this translation, allowing you to process binary data without having to worry about what might happen to combinations. It is the equivalent of using rb or wb modes when opening files with the open() function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...