sys模块:system指的是解释器(os指的是操作系统)

常用操作:用于接收系统操作系统调用解释器传入的参数
实验操作:


import sys
print(sys.argv)
#获取脚本名称 sys.argv[0]
print(sys.argv[0])
#获取脚步传递的第一个参数:
print(sys.argv[1])
#获取脚本传递的第二个参数
print(sys.argv[2])

运行成功
python之sys模块【获取参数】-LMLPHP

import os
import sys


print(sys.version)

# 作用:根据python版本编写符合不同版本的程序 使程序可兼容
if sys.version[0] == '2':
    print('running in python2...')
elif sys.version[0] == '3':
    print('running in python3...')

# 返回模块的搜索路径,初始化使用PYTHONPATH环境变量
print(sys.path)
# 可以实现跨平台
print(sys.platform)
if sys.platform == 'linux':
    os.system('ifconfig')
else:
    os.system('ipconfig')


3.6.4 (default, Aug 10 2018, 11:14:49)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
running in python3...
['/home/kiosk/PycharmProjects/2019python', '/home/kiosk/PycharmProjects/2019python', '/usr/local/python3/lib/python36.zip', '/usr/local/python3/lib/python3.6', '/usr/local/python3/lib/python3.6/lib-dynload', '/usr/local/python3/lib/python3.6/site-packages', '/usr/local/python3/lib/python3.6/site-packages/Twisted-17.1.0-py3.6-linux-x86_64.egg', '/usr/local/python3/lib/python3.6/site-packages/Automat-0.7.0-py3.6.egg', '/usr/local/python3/lib/python3.6/site-packages/constantly-15.1.0-py3.6.egg', '/usr/local/python3/lib/python3.6/site-packages/zope.interface-4.5.0-py3.6-linux-x86_64.egg', '/usr/local/python3/lib/python3.6/site-packages/attrs-18.2.0-py3.6.egg']
linux
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.1.250  netmask 255.255.255.0  broadcast 172.25.1.255
        inet6 fe80::221:ccff:febf:7aa6  prefixlen 64  scopeid 0x20<link>
        ether 00:21:cc:bf:7a:a6  txqueuelen 1000  (Ethernet)
        RX packets 1205  bytes 105121 (102.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 528  bytes 35864 (35.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s25: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:21:cc:bf:7a:a6  txqueuelen 1000  (Ethernet)
        RX packets 1208  bytes 127129 (124.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 520  bytes 41576 (40.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 20  memory 0xf2500000-f2520000

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 245  bytes 102297 (99.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 245  bytes 102297 (99.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

privbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.25.250.250  netmask 255.255.255.0  broadcast 172.25.250.255
        ether 52:54:00:9e:c5:d4  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:22:42:bc  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 52:54:00:0f:6b:dc  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.20.10.10  netmask 255.255.255.240  broadcast 172.20.10.15
        inet6 2409:8970:4c0:3ed0:d8ef:2aed:6f41:c3c2  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::2639:b9e9:1df5:6e4d  prefixlen 64  scopeid 0x20<link>
        ether 8c:70:5a:41:61:ac  txqueuelen 1000  (Ethernet)
        RX packets 49988  bytes 34571452 (32.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 47976  bytes 8449382 (8.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

--------------------练习--------------------

在命令行中,比较两文件的不同

import difflib
import sys

if len(sys.argv) !=3:
    print(
        """
        Usage: %s 比较的文件1 比较的文件2
        """%(sys.argv[0])
    )

else:
    filename1 = sys.argv[1]
    filename2 = sys.argv[2]
    try:
        with open(filename1) as f1,open(filename2) as f2:
            content1 = f1.read().splitlines(keepends=True)
            content2 = f2.read().splitlines(keepends=True)
    except Exception as e:
        print('比较错误,错误的原因',e)
    d = difflib.HtmlDiff()
    htmlContent = d.make_file(content1,content2)
    with open('different.html','w') as f:
        f.write(htmlContent)

python之sys模块【获取参数】-LMLPHP

06-28 09:14