转自:http://lizhenliang.blog.51cto.com/7876557/1872538
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
print sys.argv[0]
print sys.argv[1]
print sys.argv[2]
print sys.argv[3]
print sys.argv
print len(sys.argv)
# python test.py
test.py
a
b
c
c
['test.py', 'a', 'b', 'c']
4
>>> sys.path
['', '/usr/local/lib/python2.7/dist-packages/tornado-3.1-py2.7.egg', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
>>> sys.path.append('/opt/scripts')
>>> sys.path
['', '/usr/local/lib/python2.7/dist-packages/tornado-3.1-py2.7.egg', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/opt/scripts']
系统 | 平台标识符 |
Linux | linux |
Windows | win32 |
Windows/Cygwin | cygwin |
Mac OS X | darwin |
>>> sys.subversion
('CPython', '', '')
>>> sys.version
'2.7.6 (default, Jun 22 2015, 17:58:13) \n[GCC 4.8.2]'
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
print "Hello world!"
sys.exit()
print "Hello world!"
# python test.py
Hello world!
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
name = raw_input("Please input your name: ")
print name
# python test.py
Please input your name: xiaoming
xiaoming
1
2
3
4
5
6
7
8
9
import sys
print "Please enter your name: "
name = sys.stdin.readline()
print name
# python b.py
Please enter your name:
xiaoming
xiaoming
# cat a.py
import sys
sys.stdout.write("123456\n")
sys.stdout.flush()
# cat b.py
import sys
print sys.stdin.readlines()
# python a.py | python b.py
['123456\n']
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
print "Hello world!"
# python test.py
Hello world!
sys.stdout是有缓冲区的,比如:
import sys
import time
for i in range(5):
print i,
# sys.stdout.flush()
time.sleep(1)
# python test.py
0 1 2 3 4
>>> raise Exception, "raise..."
Traceback (most recent call last):File "<stdin>", line 1, in <module>
Exception: raise...