天天看點

Python白手起家四:系統變量與子產品

為了實作書上的一個例子,代碼檔案儲存為mytest.py:

import sys

print('指令行參數是:')

for i in sys.argv:

    print(i)

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環境下,執行

>>> help(sys)

Help on built-in module sys:

NAME

    sys

.............................

可以看到子產品中有很多内容,當然包括了

Dynamic objects:

    argv -- command line arguments; argv[0] is the script pathname if known

    path -- module search path; path[0] is the script directory, else ''

    modules -- dictionary of loaded modules

.................

這樣的話,說明動态對象有argv、path和modulues等多個。

然後我看到了還有一個靜态對象,其中:

Static objects:

    float_info -- a dict with information about the float implementation.

    int_info -- a struct sequence with information about the int implementation.

    maxsize -- the largest supported length of containers.

    maxunicode -- the largest supported character

    builtin_module_names -- tuple of module names built into this interpreter

    subversion -- subversion information of the build as tuple

    version -- the version of this interpreter as a string

    version_info -- version information as a named tuple

    hexversion -- version information encoded as a single integer

    copyright -- copyright notice pertaining to this interpreter

    platform -- platform identifier

.........................

有個platform的靜态參數,不用說一定是顯示作業系統平台的意思,于是便試試,在Python控制台:

>>> import sys

>>> print(sys.platform)

win32

>>>

看,顯示的win32。當然還有FUNCTIONS和FILE,大家可以自己試試。

<b>建議:</b>

<b>    多看自帶的文檔,才能更快地成熟起來。人總要學着自己長大,才能獨立。光靠看教程,吃别人喂得東西遠遠不夠。自己探究,自己嘗試,自己排錯,才能讓自己真正去了解,學到的才是明白的東西。</b>