首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

请问一下sys模块的有关问题

2014-01-21 
请教一下sys模块的问题#!/usr/bin/python# Filename: using_sys.pyimport sysprint The command line arg

请教一下sys模块的问题

#!/usr/bin/python
# Filename: using_sys.py

import sys

print 'The command line arguments are:'
for i in sys.argv:
    print i

print '\n\nThe PYTHONPATH is', sys.path, '\n' 


输出
$ python using_sys.py we are arguments
The command line arguments are:
using_sys.py
we
are
arguments


The PYTHONPATH is ['/home/swaroop/byte/code', '/usr/lib/python23.zip',
'/usr/lib/python2.3', '/usr/lib/python2.3/plat-linux2',
'/usr/lib/python2.3/lib-tk', '/usr/lib/python2.3/lib-dynload',
'/usr/lib/python2.3/site-packages', '/usr/lib/python2.3/site-packages/gtk-2.0']



可是我输出的
The command line arguments are:
C:\Python23\using_sys.py


The PYTHONPATH is ['C:\\Python23\\Lib\\idlelib', 'C:\\Python23\\python23.zip', 'C:\\Python23', 'C:\\Python23\\DLLs', 'C:\\Python23\\lib', 'C:\\Python23\\lib\\plat-win', 'C:\\Python23\\lib\\lib-tk', 'C:\\Python23\\lib\\site-packages'] 

我的输出和例子的输出不一样。后来我发现,只要是关于sys模块的例子,我的输出都和例子的输出不一样,求解
[解决办法]
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

不知道你说的不一样是指什么?例子是在*nux里执行的,你的是在windows下执行的,有差异不是很正常吗

这我知道,不过我的输出为什么没有
we
are
arguments
只是因为系统的不一样吗?

这是另一个例子
#!/usr/bin/python
# Filename: cat.py

import sys

def readfile(filename):
    '''Print a file to the standard output.'''
    f = file(filename)
    while True:
        line = f.readline()
        if len(line) == 0:
            break
        print line, # notice comma
    f.close()

# Script starts from here
if len(sys.argv) < 2:
    print 'No action specified.'
    sys.exit()

if sys.argv[1].startswith('--'):
    option = sys.argv[1][2:]
    # fetch sys.argv[1] but without the first two characters
    if option == 'version':
        print 'Version 1.2'
    elif option == 'help':
        print '''\
This program prints files to the standard output.
Any number of files can be specified.
Options include:
  --version : Prints the version number
  --help    : Display this help'''
    else:
        print 'Unknown option.'
    sys.exit()
else:
    for filename in sys.argv[1:]:
        readfile(filename) 


然后输出
$ python cat.py
No action specified.

$ python cat.py --help
This program prints files to the standard output.
Any number of files can be specified.
Options include:
  --version : Prints the version number
  --help    : Display this help

$ python cat.py --version
Version 1.2

$ python cat.py --nonsense
Unknown option.

$ python cat.py poem.txt
Programming is fun
When the work is done
if you wanna make your work also fun:
        use Python!



而我的电脑输出的却是
No action specified.

Traceback (most recent call last):


  File "C:\Python23\cat.py", line 19, in -toplevel-
    sys.exit()
SystemExit


这让我十分不解,感觉差别太大了,完全看不懂

你这进第19行这个分支里去了,没输入参数,然后就退出了。有啥问题?

这只是个使用sys.argv的例子,为什么会没有输入参数?

参数是你在命令行下执行这个文件的时候自己敲的,你不输入参数,参数何来?

热点排行