python 基礎 7.6 sys 模組

來源:互聯網
上載者:User

標籤:rip   1.7   參數   pen   定向   txt   退出   col   stdin   

一.sys 模組sys 模組主要功能是擷取參數  [[email protected] pythonscripts]# cat 2.py #!/usr/bin/python#coding=utf-8 import os import sys if  __name__ == ‘__main__‘:    print (‘sys.argv[0]= {0}‘.format(sys.argv[0]))    print (‘sys.argv[1]= {0}‘.format(sys.argv[1]))    print (‘sys.argv[2]= {0}‘.format(sys.argv[2])) 運行結果:[[email protected] pythonscripts]# python 2.py 1 1.txtsys.argv[0]= 2.pysys.argv[1]= 1sys.argv[2]= 1.txt  #2 . sys.stdint  sys.stdin  sys.stderr 可以更好的控制輸入輸出(換行).stdout等是預設不換行的。print ‘aa‘*2sys.stdout.write(‘hello 劉振川‘)   print (‘hello world‘) >>>aaaahello 劉振川hello world    print ‘####sys.stdin 換行####‘##讓 sys.stdout sys.stdin   sys.err 預設換行,需要加分行符號。print ‘aa‘*2sys.stdout.write(‘hello 劉振川\n‘)print (‘hello world‘) >>>####sys.stdin 換行####aaaahello 劉振川hello world  #3 .從控制台重新導向到檔案. 在當前檔案下產生一個新的1.log ,檔案內容為 hellof = open(‘1.log‘,‘w‘)sys.stdout = fprint ‘hello‘  #4. sys.exit(n) 退出.。 功能:執行到主程式末尾,解譯器自動結束。0是正常退出,非0數不正常退出                                                                                          

python 基礎 7.6 sys 模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.