Skip to Content
Python 技術手冊 第三版
book

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced content levelIntermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
362
|
10 章 檔案與文字作業
ValueError
;它永遠都不會有任何副作用( side effect)。然而,要確保
完全的安全性,在這種情況中,
astring
不能使用任何運算子,也不能用任
何非關鍵字的識別字。舉例來說:
import ast
print(ast.literal_eval('23')) # 印出 23
print(ast.literal_eval('[2,3]')) # 印出 [2, 3]
print(ast.literal_eval('2+3')) # 提出 ValueError
print(ast.literal_eval('2+')) # 提出 SyntaxError
getpass 模組
在非常少數的情況下,你可能會希望使用者輸入文字行的時候,看得到螢
幕的其他人無法看見使用者在打什麼。這可能發生在你向使用者請求密碼
password)的時候。
getpass
模組提供下列函式:
getpass getpass(prompt='Password: ')
就像
input
,只不過在使用者輸入的過程中,文字不會回應(echo)到螢
幕上。
getpass
預設的
prompt
input
的不同。
getuser getuser()
回傳目前使用者的使用者名稱(username)。
getuser
會試著從環境變數
LOGNAME
USER
LNAME
USERNAME
的值取得使用者名稱,依列出的順序進
行。如果
os.environ
中沒有這些變數,
getuser
就會詢問作業系統。
更豐富的文字 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

測試驅動開發|使用 Python

測試驅動開發|使用 Python

Harry J.W. Percival
Linux 内核观测技术BPF

Linux 内核观测技术BPF

David Calavera, Lorenzo Fontana

Publisher Resources

ISBN: 9789864766819