python如何判斷stdin裡面是否有資料?

時間 2021-06-04 11:07:56

1樓:呵呵一笑百媚生

fcntl

import

osimport

sys# 非阻塞模式fd=

sys.

stdin

.fileno

()fl

=fcntl

.fcntl(fd

,fcntl

.F_GETFL

)fcntl

.fcntl(fd

,fcntl

.F_SETFL,fl

|os.O_NONBLOCK

)try

:for

line

insys

.stdin

:print

line

except

IOError

:# 如果沒資料會直接拋異常

pass

print233

2樓:

可以通過 sys.stdin.isatty() 判斷是否是管道和重定向輸入(為 True 時表示是互動式環境, 為 False 時管道和重定向輸入)。

data

=sys

.argv[1

:]if

notsys

.stdin

.isatty

():data.(

sys.

stdin

.read

())print

(' '

.join

(data))

c 如何多次重定向stdin?

小蔡蔡 需要cin.clear 我建議直接 while cin 這樣能捕捉到的異常更多,所以一般寫 while cin a C 有3個標準流 stdin 標準輸入流 stdout 標準輸出流 stderr 標準錯誤流 預設情況下,它們都指向螢幕。使用freopen,可以重定向它們。比如freopen...

Python如何判斷列表元素嚴格遞增?

Beviniy 提供一種效率更高的方案 all a i a i 1 for i in range len a 1 原答案sorted a a and len set a len a defjudge alist if any alist i 1 alist i foriin range 0 len ...

python如何判斷連續滿足條件的區間?

這不難啊很容易想到乙個O n 的演算法 前面我就不寫了 假設陣列存在l中 list是保留字啊怎麼用來做變數名的 l 1,0 1,1 1,1 1,0 1,1 1,0 0,0 1,1 1,1 1,1 1,0 0,0 1,1 0,1 1,0 1 dry l fori inrange 5 len l 把l中...