sys module: Full System, refers to the interpreter (os refers to the operating system)
common operations, the system for receiving an operating system call parameters passed interpreter
1.sys.argv # script to get all the parameters passed, returns a list; the parameters of all elements in the list are the script passed
sys.argv [0] # Get the first argument passed footsteps; fixed script name
sys. argv [1] # Get the second parameter passed pace
may be obtained by passing the script parameters indexed manner
2.sys.version interpreter version information, it returns a string
effect: the preparation of python conforming to different versions of the program version make the program compatible
import sys
import os
print(sys.version)
运行:
3.6.4 (default, Aug 10 2018, 11:14:49)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
It may also be explained by the characteristics of the version number string slice
import sys
import os
print(sys.version)
print(sys.version[:5])
运行:
3.6.4 (default, Aug 10 2018, 11:14:49)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
3.6.4
3, sys.path # return the module search path, use the PYTHONPATH environment variable initialization
import os
import sys
print(sys.path)
运行:
['/home/kiosk/PycharmProjects/20190523/20190524', '/home/kiosk/PycharmProjects/20190523', '/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']
4.sys.platform # can achieve cross-platform
import sys
import os
# print(sys.version)
# print(sys.version[:5])
# print(sys.path)
print(sys.platform)
if sys.platform == 'linux':
os.system('linux系统的命令')
else:
os.system('其他系统的命令')
Application Exercise:
Requirements:
Scripting: compare.py run python in the command file 1 file 2 output shows the difference of the two documents at
import difflib
import sys
方法一:
parameter = sys.argv
with open(parameter[1]) as f1,open (parameter[2]) as f2:
file1 = f1.read().splitlines(keepends=True)
file2 = f2.read().splitlines(keepends=True)
diff = difflib.Differ()
diff_result = diff.compare(file1,file2)
print(''.join(list(diff_result)))
方法二:
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)
运行结果:
[kiosk@foundation40 20190524]$ /usr/local/python3/bin/python3 compare.py /home/kiosk/file /home/kiosk/file1
hello
+
+ mewfw4t
+ 4t5tr
+ fcdfbv
+ efr4