python常用模块-sys

Python常用模块-sys

https://www.cnblogs.com/pycode/p/sysos.html

https://www.cnblogs.com/BigFishFly/p/6622784.html

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]

sys.argv 

[admin@pe-jira wangxu]$  cat s.py 
import sys
print(sys.argv[0])
print(sys.argv[1])
print(sys.argv[1:])
print(sys.argv[2:])
[admin@pe-jira wangxu]$ python s.py 1 2 3 4 5
s.py
1
['1', '2', '3', '4', '5']
['2', '3', '4', '5']
[admin@pe-jira wangxu]$

sys.exit(n)

>>> import sys
>>> sys.exit()
[admin@pe-jira wangxu]

sys.version

>>> import sys 
>>> 
>>> sys.version
'3.6.0 (default, May 28 2018, 13:50:58) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)]'
>>>

sys.stdin.read

[admin@pe-jira wangxu]$ cat read.py       
#!/usr/bin/env python
#encoding=utf-8
import sys
for line in sys.stdin.read():
    print(line.strip())

[admin@pe-jira wangxu]$ cat source.txt 
111 222
333
[admin@pe-jira wangxu]$ python read.py < source.txt             
1
1
1

2
2
2

3
3
3
[admin@pe-jira wangxu]$

sys.stdin.readline
[admin@pe-jira wangxu]$ cat read.py 
#!/usr/bin/env python
#encoding=utf-8
import sys
for line in sys.stdin.readline():
    print(line.strip())

[admin@pe-jira wangxu]$  cat source.txt 
111 222
333
[admin@pe-jira wangxu]$ python read.py < source.txt     
1
1
1

2
2
2

[admin@pe-jira wangxu]$

sys.stdin.readlines

[admin@pe-jira wangxu]$ cat read.py 
#!/usr/bin/env python
#encoding=utf-8
import sys
for line in sys.stdin.readlines()[:-1]:
    print(line.strip())

[admin@pe-jira wangxu]$ cat source.txt 
111 222
333
[admin@pe-jira wangxu]$ python read.py < source.txt    
111 222
[admin@pe-jira wangxu]$
[admin@pe-jira wangxu]$ cat read.py                 
#!/usr/bin/env python
#encoding=utf-8
import sys
for line in sys.stdin.readlines():
    print(line.strip())

[admin@pe-jira wangxu]$ cat source.txt              
111 222
333
[admin@pe-jira wangxu]$ python read.py < source.txt    
111 222
333
[admin@pe-jira wangxu]$


sys.stdin.readline只能输入一行
>>> m = sys.stdin.read()     
sssssss
ssssss
sss
ddd
>>> print(m)            
sssssss
ssssss
sss
ddd

>>> m = sys.stdin.readline() 
222
>>> print(m)                
222

>>> m = sys.stdin.readlines()
2222
333
444
>>> print(m)
['2222\n', '333\n', '444\n']
>>>

sys.stdout.write

http://www.cnblogs.com/valleyofwind/p/8628925.html

https://www.cnblogs.com/pycode/p/sysos.html

①sys.stdout.write是将str写到流,原封不动,不会像print那样默认end='\n'

②sys.stdout.write只能输出一个str,而print能输出多个str,且默认sep=' '(一个空格)

③print,默认flush=False.

④print还可以直接把值写到file中

>>> sys.stdout.write('aaa') 
aaa3
>>> sys.stdout.write('aaa\n')
aaa
4
>>> sys.stdout.write('aaa\r')
4aa
>>> sys.stdout.write('aaa\n')
aaa
4
>>> print('aaa','sss')
aaa sss
>>> sys.stdout.write('aaa','22222')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: write() takes exactly one argument (2 given)
>>>

进度条
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#pyversion:python3.5
#owner:fuzj


"""
sys 和python解析器相关
"""

import sys
import time



def view_bar(num,total):

    rate = num / total
    rate_num =  int(rate * 100)
    #r = '\r %d%%' %(rate_num)
    r = '\r%s>%d%%' % ('=' * rate_num, rate_num,)
    sys.stdout.write(r)
    sys.stdout.flush


if __name__ == '__main__':
    for i in range(0, 101):
        time.sleep(0.1)
        view_bar(i, 100)
效果:

====================================================================================================>100%

 

sys.stdout.flush

flush是刷新的意思,在print和sys.stdout.write输出时是有一个缓冲区的。

比如要向文件里输出字符串,是先写进内存(因为print默认flush=False,也没有手动执行flush的话),在close文件之前直接打开文件是没有东西的,如果执行一个flush就有了。

>>> import time
>>> import sys
>>>  
... for i in range(5):
...     print(i)
...     sys.stdout.flush()
...     time.sleep(1)
... 
0
1
2
3
4
>>>

 

 

posted on 2018-05-31 15:06  光阴8023  阅读(206)  评论(0编辑  收藏  举报