天天看點

簡明Python教程學習筆記_8_sys和osos子產品

<code>sys</code>子產品包含系統對應的功能。我們已經學習了<code>sys.argv</code>清單,它包含指令行參數。

<code>#!/usr/bin/python # filename: cat.py</code>

<code>import</code><code>sys</code>

<code>def</code><code>readfile</code><code>(filename):</code>

<code>  '''print a file to the standard output.'''</code>

<code>  f =</code><code>file</code><code>(filename)</code>

<code>  while</code><code>true</code><code>:     line = f.readline()</code>

<code>    if</code><code>len</code><code>(line) ==</code>

<code>0</code><code>:</code>

<code>      break</code>

<code>    print</code><code>line,</code><code># notice comma</code>

<code>  f.close()</code>

<code># script starts from here</code>

<code>if</code><code>len</code><code>(</code><code>sys</code><code>.argv) &lt;</code><code>2</code><code>:</code>

<code>  print</code><code>'no action specified.'</code>

<code>  sys</code><code>.exit()</code>

<code>if</code><code>sys</code><code>.argv[</code><code>1</code><code>].startswith(</code><code>'--'</code><code>):</code>

<code>  option =</code><code>sys</code><code>.argv[</code><code>1</code><code>][</code><code>2</code><code>:]</code>

<code>  # fetch sys.argv[1] but without the first two characters</code>

<code>  if</code><code>option ==</code><code>'version'</code><code>:</code>

<code>    print</code><code>'version 1.2'</code>

<code>  elif</code><code>option ==</code><code>'help'</code><code>:</code>

<code>    print</code><code>'''\ 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'''</code>

<code>  else</code><code>:</code>

<code>    print</code><code>'unknown option.'</code>

<code>else</code><code>:</code>

<code>  for</code><code>filename</code><code>in</code>

<code>sys</code><code>.argv[</code><code>1</code><code>:]:     readfile(filename)</code>

輸出

<code>$ 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!</code>

這個程式用來模範linux/unix使用者熟悉的cat指令。你隻需要指明某些文本檔案的名字,這個程式會把它們列印輸出。

在python程式運作的時候,即不是在互動模式下,在<code>sys.argv</code>清單中總是至少有一個項目。它就是目前運作的程式名稱,作為<code>sys.argv[0]</code>(由于python從<code>0</code>開始計數)。其他的指令行參數在這個項目之後。

為了使這個程式對使用者更加友好,我們提供了一些使用者可以指定的選項來了解更多程式的内容。我們使用第一個參數來檢驗我們的程式是否被指定了選項。如果使用了<code>--version</code>選項,程式的版本号将被列印出來。類似地,如果指定了<code>--help</code>選項,我們提供一些關于程式的解釋。我們使用<code>sys.exit</code>函數退出正在運作的程式。和以往一樣,你可以看一下<code>help(sys.exit)</code>來了解更多詳情。

如果沒有指定任何選項,而是為程式提供檔案名的話,它就簡單地列印出每個檔案地每一行,按照指令行中的順序一個檔案接着一個檔案地列印。

順便說一下,名稱cat是 concatenate 的縮寫,它基本上表明了程式的功能——它可以在輸出列印一個檔案或者把兩個或兩個以上檔案連接配接/級連在一起列印。

對于有經驗的程式員,<code>sys</code>子產品中其他令人感興趣的項目有<code>sys.stdin</code>、<code>sys.stdout</code>和<code>sys.stderr</code>它們分别對應你的程式的标準輸入、标準輸出和标準錯誤流。

這個子產品包含普遍的作業系統功能。如果你希望你的程式能夠與平台無關的話,這個子產品是尤為重要的。即它允許一個程式在編寫後不需要任何改動,也不會發生任何問題,就可以在linux和windows下運作。一個例子就是使用<code>os.sep</code>可以取代作業系統特定的路徑分割符。

下面列出了一些在<code>os</code>子產品中比較有用的部分。它們中的大多數都簡單明了。

<code>os.name</code>字元串訓示你正在使用的平台。比如對于windows,它是<code>'nt'</code>,而對于linux/unix使用者,它是<code>'posix'</code>。

<code>os.getcwd()</code>函數得到目前工作目錄,即目前python腳本工作的目錄路徑。

<code>os.getenv()</code>和<code>os.putenv()</code>函數分别用來讀取和設定環境變量。

<code>os.listdir()</code>傳回指定目錄下的所有檔案和目錄名。

<code>os.remove()</code>函數用來删除一個檔案。

<code>os.system()</code>函數用來運作shell指令。

<code>os.linesep</code>字元串給出目前平台使用的行終止符。例如,windows使用<code>'\r\n'</code>,linux使用<code>'\n'</code>而mac使用<code>'\r'</code>。

<code>os.path.split()</code>函數傳回一個路徑的目錄名和檔案名。

<code>&gt;&gt;&gt; os.path.split('/home/swaroop/byte/code/poem.txt') ('/home/swaroop/byte/code', 'poem.txt')</code>

<code>os.path.isfile()</code>和<code>os.path.isdir()</code>函數分别檢驗給出的路徑是一個檔案還是目錄。類似地,<code>os.path.existe()</code>函數用來檢驗給出的路徑是否真地存在。

你可以利用python标準文檔去探索更多有關這些函數和變量的詳細知識。你也可以使用<code>help(sys)</code>等等。