sys模块

sys模块

sys.argv

接受命令行传递的参数
第一个参数时文件名称,后续为参数

脚本内容:
#!/usr/bin/python3
import sys

name="hello world"
print(name)

print("-------------------------------");
print("The list of command line arguments:\n", sys.argv)

print("-------------------------------");
for i in sys.argv:
  print(i)



输出:
[root@localhost scripts]# python3 first.py hello1 hello2
hello world
-------------------------------
The list of command line arguments:
 ['first.py', 'hello1', 'hello2']
-------------------------------
first.py
hello1
hello2

sys.platform

在linux环境下:
sys.platform  为linux

在windows环境下:
sys.platform  为win32

sys.executable

脚本:
#!/usr/bin/python3
import sys


name="hello world"
print(name)

print("-------------------------------");
print("The list of command line arguments:\n", sys.argv)

print("-------------------------------");
for i in sys.argv:
  print(i)

print(sys.platform)

print(sys.executable)



输出:
[root@localhost scripts]# python3 first.py hello1 hello2
hello world
-------------------------------
The list of command line arguments:
 ['first.py', 'hello1', 'hello2']
-------------------------------
first.py
hello1
hello2
linux
/usr/bin/python3

sys.modules

脚本:
#!/usr/bin/python3
import sys


name="hello world"
print(name)

print("-------------------------------");
print("The list of command line arguments:\n", sys.argv)

print("-------------------------------");
for i in sys.argv:
  print(i)

print(sys.platform)

print(sys.executable)


print("-------------------------------");
print(sys.modules)


输出:
[root@localhost scripts]# python3 first.py hello1 hello2
hello world
-------------------------------
The list of command line arguments:
 ['first.py', 'hello1', 'hello2']
-------------------------------
first.py
hello1
hello2
linux
/usr/bin/python3
-------------------------------
{'builtins': <module 'builtins' (built-in)>, 'sys': <module 'sys' (built-in)>, '_frozen_importlib': <module '_frozen_importlib' (frozen)>, '_imp': <module '_imp' (built-in)>, '_warnings': <module '_warnings' (built-in)>, '_thread': <module '_thread' (built-in)>, '_weakref': <module '_weakref' (built-in)>, '_frozen_importlib_external': <module '_frozen_importlib_external' (frozen)>, '_io': <module 'io' (built-in)>, 'marshal': <module 'marshal' (built-in)>, 'posix': <module 'posix' (built-in)>, 'zipimport': <module 'zipimport' (built-in)>, 'encodings': <module 'encodings' from '/usr/lib64/python3.6/encodings/__init__.py'>, 'codecs': <module 'codecs' from '/usr/lib64/python3.6/codecs.py'>, '_codecs': <module '_codecs' (built-in)>, 'encodings.aliases': <module 'encodings.aliases' from '/usr/lib64/python3.6/encodings/aliases.py'>, 'encodings.utf_8': <module 'encodings.utf_8' from '/usr/lib64/python3.6/encodings/utf_8.py'>, '_signal': <module '_signal' (built-in)>, '__main__': <module '__main__' from 'first.py'>, 'encodings.latin_1': <module 'encodings.latin_1' from '/usr/lib64/python3.6/encodings/latin_1.py'>, 'io': <module 'io' from '/usr/lib64/python3.6/io.py'>, 'abc': <module 'abc' from '/usr/lib64/python3.6/abc.py'>, '_weakrefset': <module '_weakrefset' from '/usr/lib64/python3.6/_weakrefset.py'>, 'site': <module 'site' from '/usr/lib64/python3.6/site.py'>, 'os': <module 'os' from '/usr/lib64/python3.6/os.py'>, 'errno': <module 'errno' (built-in)>, 'stat': <module 'stat' from '/usr/lib64/python3.6/stat.py'>, '_stat': <module '_stat' (built-in)>, 'posixpath': <module 'posixpath' from '/usr/lib64/python3.6/posixpath.py'>, 'genericpath': <module 'genericpath' from '/usr/lib64/python3.6/genericpath.py'>, 'os.path': <module 'posixpath' from '/usr/lib64/python3.6/posixpath.py'>, '_collections_abc': <module '_collections_abc' from '/usr/lib64/python3.6/_collections_abc.py'>, '_sitebuiltins': <module '_sitebuiltins' from '/usr/lib64/python3.6/_sitebuiltins.py'>, 'sysconfig': <module 'sysconfig' from '/usr/lib64/python3.6/sysconfig.py'>, '_sysconfigdata_m_linux_x86_64-linux-gnu': <module '_sysconfigdata_m_linux_x86_64-linux-gnu' from '/usr/lib64/python3.6/_sysconfigdata_m_linux_x86_64-linux-gnu.py'>}

sys.path

该属性是一个由字符串组成的列表,其中各个元素表示的是 Python 搜索模块的路径;在程序启动期间被初始化。

其中第一个元素(也就是path[0])的值是最初调用 Python 解释器的脚本所在的绝对路径;如果是在交互式环境下查看sys.path的值,就会得到一个空字符串。

sys.path

输出:
['/root/scripts', '/usr/lib64/python36.zip', '/usr/lib64/python3.6', '/usr/lib64/python3.6/lib-dynload', '/usr/lib64/python3.6/site-packages', '/usr/lib/python3.6/site-packages']

sys.exit

功能:执行到主程序末尾,解释器自动退出,但是如果需要中途退出程序,可以调用sys.exit函数,带有一个可选的整数参数返回给调用它的程序,表示你可以在主程序中捕获对sys.exit的调用。(0是正常退出,其他为异常)

sys.stdin sys.stdout sys.err

posted @ 2022-05-30 17:09  梁永旺  阅读(34)  评论(0编辑  收藏  举报