Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4020942
  • 博文数量: 272
  • 博客积分: 7846
  • 博客等级: 少将
  • 技术积分: 6476
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-25 16:27
文章分类

全部博文(272)

分类: Python/Ruby

2011-04-17 09:23:00

为了实现书上的一个例子,代码文件保存为mytest.py:
  1. import sys

  2. print('命令行参数是:')

  3. for i in sys.argv:
  4.     print(i)

  5. print('\n\nPython Path is:',sys.path,'\n')
如果是Linux下,则非常简单,只需要python mytest.py n1 n2 n3

但在XP下,就得稍作调整:
1、查看Python系统路径,我使用3.2版本,默认装到了C:根目录下,即C:\Python32
2、右键“我的电脑”->“属性”->“高级”->“环境变量”->编辑“Path”->“变量值”->末尾插入“;C:\Python32”(注意C:之前的那个分号连接符)->“确定”
3、“win + r”->“CMD”打开XP下的命令行界面
4、进入Python程序所在目录,我的是桌面上的Python文件夹
5、执行命令python mytest.py n1 n2 n3

显示以下结果:
C:\Documents and Settings\Administrator\桌面\Python>python mytest.py ni wo ta
命令行参数是:
mytest.py
ni
wo
ta


Python Path is: ['C:\\Documents and Settings\\Administrator\\桌面\\Python', 'C:\\WINDOWS\\system32\\python32.zip', 'C:\\Python32\\DLLs', 'C:\\Python32\\lib', 'C:\\Python32', 'C:\\Python32\\lib\\site-packages']

好了,为了了解sys的用途,在IDLE或任何Python环境下,执行
  1. >>> help(sys)
  2. Help on built-in module sys:

  3. NAME
  4.     sys
  5. .............................
可以看到模块中有很多内容,当然包括了
  1. Dynamic objects:
  2.     
  3.     argv -- command line arguments; argv[0] is the script pathname if known
  4.     path -- module search path; path[0] is the script directory, else ''
  5.     modules -- dictionary of loaded modules
  6.     
  7. .................
这样的话,说明动态对象有argv、path和modulues等多个。
然后我看到了还有一个静态对象,其中
  1. Static objects:
  2.     
  3.     float_info -- a dict with information about the float implementation.
  4.     int_info -- a struct sequence with information about the int implementation.
  5.     maxsize -- the largest supported length of containers.
  6.     maxunicode -- the largest supported character
  7.     builtin_module_names -- tuple of module names built into this interpreter
  8.     subversion -- subversion information of the build as tuple
  9.     version -- the version of this interpreter as a string
  10.     version_info -- version information as a named tuple
  11.     hexversion -- version information encoded as a single integer
  12.     copyright -- copyright notice pertaining to this interpreter
  13.     platform -- platform identifier
  14. .........................
有个platform的静态参数,不用说一定是显示操作系统平台的意思,于是便试试,在Python控制台:
  1. >>> import sys
  2. >>> print(sys.platform)
  3. win32
  4. >>>
看,显示的win32。当然还有FUNCTIONS和FILE,大家可以自己试试。

建议:
    多看自带的文档,才能更快地成熟起来。人总要学着自己长大,才能独立。光靠看教程,吃别人喂得东西远远不够。自己探究,自己尝试,自己排错,才能让自己真正去了解,学到的才是明白的东西。
阅读(4326) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~