Python sys模块
Python sys模块
sys模块
用于提供对解释器相关的操作
sys.argv
# 命令行参数List,第一个元素是程序本身路径
sys.exit(n)
# 退出程序,正常退出时exit(0)
sys.version
# 获取Python解释程序的版本信息
sys.maxint
# 最大的Int值
sys.path
# 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform
# 返回操作系统平台名称
sys.stdout.write('please:')
val = sys.stdin.readline()[:-1]
测试脚本
import sys
print(sys.stdout.write("please:")) # 进度条使用
val = sys.stdin.readline()[:-1] # 读取键盘的一次输入
print(sys.platform) # 返回操作系统平台名称
print(sys.path) # 返回模块的搜索路径
print(sys.version) # 获取Python解释程序的版本信息
print(sys.argv) # 命令行参数List,第一个元素是程序本身路径
# 输出结果
please:7
darwin
['/Users/evescn/Documents/GitHub/python', '/Users/evescn/Documents/GitHub/python', '/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm_display', '/Users/evescn/.conda/envs/python/lib/python310.zip', '/Users/evescn/.conda/envs/python/lib/python3.10', '/Users/evescn/.conda/envs/python/lib/python3.10/lib-dynload', '/Users/evescn/.conda/envs/python/lib/python3.10/site-packages', '/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm_matplotlib_backend']
3.10.11 (main, May 15 2023, 19:29:30) [Clang 14.0.6 ]
['/Users/evescn/Documents/GitHub/python/1.py']
进度条功能实现
import sys, time
for i in range(20):
sys.stdout.write("#")
sys.stdout.flush()
time.sleep(0.1)
import time
def progress(precent, width=50):
if precent > 1:
precent = 1
show_str = ('[%%-%ds]' % width) % ('#' * int(precent * width))
print('\r%s %d%%' % (show_str, int(precent * 100)), end="")
recv_size = 0
total_size = 1231234
while recv_size < total_size:
time.sleep(0.1)
recv_size += 1024
progress(recv_size / total_size)
### 运行结果 ###
[############# ] 27%