python sys模块_Python 基础(二十):sys 模块
1. 簡介
sys 模塊主要負責與 Python 解釋器進行交互,該模塊提供了一系列用于控制 Python 運行環境的函數和變量。
之前我們說過 os 模塊,該模塊與 sys 模塊從名稱上看著好像有點類似,實際上它們之間是沒有什么關系的,os 模塊主要負責與操作系統進行交互。
2. 使用
我們先整體看一下 sys 模塊都包含哪些內容,如下所示:
>>> import sys >>> dir(sys) ['__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_enablelegacywindowsfsencoding', '_getframe', '_git', '_home', '_xoptions', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'set_asyncgen_hooks', 'set_coroutine_wrapper', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions', 'winver']對于一些相對常用的變量和函數,我們下面再來具體看一下。
argv
返回傳遞給 Python 腳本的命令行參數列表。看下示例:
import sysif __name__ == '__main__':args = sys.argvprint(args)print(args[1])上面文件名為:test.py,我們在控制臺使用命令:python test.py 123 abc 執行一下,執行結果如下:
['test.py', '123', 'abc'] 123version
返回 Python 解釋器的版本信息。
winver
返回 Python 解釋器主版號。
platform
返回操作系統平臺名稱。
path
返回模塊的搜索路徑列表。
maxsize
返回支持的最大整數值。
maxunicode
返回支持的最大 Unicode 值。
copyright
返回 Python 版權信息。
modules 以字典類型返回系統導入的模塊。
byteorder
返回本地字節規則的指示器。
executable
返回 Python 解釋器所在路徑。
import sysprint(sys.version) print(sys.winver) print(sys.platform) print(sys.path) print(sys.maxsize) print(sys.maxunicode) print(sys.copyright) print(sys.modules) print(sys.byteorder) print(sys.executable)stdout
標準輸出。看下示例:
import sys# 下面兩行代碼等價 sys.stdout.write('Hi' + 'n') print('Hi')stdin
標準輸入。看下示例:
import syss1 = input() s2 = sys.stdin.readline() print(s1) print(s2)stderr
錯誤輸出。看下示例:
import syssys.stderr.write('this is a error message')exit()
退出當前程序。看下示例:
import sysprint('Hi') sys.exit() print('Jhon')getdefaultencoding()
返回當前默認字符串編碼的名稱。
getrefcount(obj)
返回對象的引用計數。
getrecursionlimit()
返回支持的遞歸深度。
getsizeof(object[, default])
以字節為單位返回對象的大小。
setswitchinterval(interval)
設置線程切換的時間間隔。
getswitchinterval()
返回線程切換時間間隔。
import sysprint(sys.getdefaultencoding()) print(sys.getrefcount('123456')) print(sys.getrecursionlimit()) print(sys.getsizeof('abcde')) sys.setswitchinterval(1) print(sys.getswitchinterval())總結
以上是生活随笔為你收集整理的python sys模块_Python 基础(二十):sys 模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: request.getParameter
- 下一篇: python实时读取日志并打印关键字怎么