<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # sys模塊 `sys`模塊包含系統對應的功能。我們已經學習了`sys.argv`列表,它包含命令行參數。 ``` #!/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) &lt; 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) ``` (源文件:[code/cat.py](code/cat.py)) ## 輸出 ``` $ 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! ``` ## 它如何工作 這個程序用來模范Linux/Unix用戶熟悉的**cat**命令。你只需要指明某些文本文件的名字,這個程序會把它們打印輸出。 在Python程序運行的時候,即不是在交互模式下,在`sys.argv`列表中總是至少有一個項目。它就是當前運行的程序名稱,作為`sys.argv[0]`(由于Python從`0`開始計數)。其他的命令行參數在這個項目之后。 為了使這個程序對用戶更加友好,我們提供了一些用戶可以指定的選項來了解更多程序的內容。我們使用第一個參數來檢驗我們的程序是否被指定了選項。如果使用了`--version`選項,程序的版本號將被打印出來。類似地,如果指定了`--help`選項,我們提供一些關于程序的解釋。我們使用`sys.exit`函數退出正在運行的程序。和以往一樣,你可以看一下`help(sys.exit)`來了解更多詳情。 如果沒有指定任何選項,而是為程序提供文件名的話,它就簡單地打印出每個文件地每一行,按照命令行中的順序一個文件接著一個文件地打印。 順便說一下,名稱**cat**是 concatenate 的縮寫,它基本上表明了程序的功能——它可以在輸出打印一個文件或者把兩個或兩個以上文件連接/級連在一起打印。 `sys.version`字符串給你提供安裝的Python的版本信息。`sys.version_info`元組則提供一個更簡單的方法來使你的程序具備Python版本要求功能。 ``` [swaroop@localhost code]$ python >>> import sys >>> sys.version '2.3.4 (#1, Oct 26 2004, 16:42:40) \n[GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)]' >>> sys.version_info (2, 3, 4, 'final', 0) ``` 對于有經驗的程序員,`sys`模塊中其他令人感興趣的項目有`sys.stdin`、`sys.stdout`和`sys.stderr`它們分別對應你的程序的標準輸入、標準輸出和標準錯誤流。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看