我,python,也要一行程式
思路這種東西,對於非科班出身的我來說,太重要了
因為我不會捧著厚厚的一本書,例如《python從入門到放棄》,什麼的去看。自然不會系統理論的去學習
通常是遇到問題,我就去百度,百度不到,我就google。直到找到適合的方法。
所以說,對於python,我不會像shell,awk,perl那樣去敲一行程式,都是寫成xxx.py的指令碼去執行。
但是對於嚴酷的生產環境來說,你在伺服器上做太多個人的指令碼顯然是給自己挖坑。
秉承快速理念,今天研究一下Python的一行程式。契機源於知乎上看到的一篇文章。
首先python一行程式的引數是什麼,遇到這些問題請像我一樣,命令加-h去檢視
PS C:\Users\Administrator> python -h
usage: C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-b : issue warnings about str(bytes_instance), str(bytearray_instance)
and comparing bytes/bytearray with str. (-bb: issue errors)
-B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x
-c cmd : program passed in as string (terminates option list)
-d : debug output from parser; also PYTHONDEBUG=x
-E : ignore PYTHON* environment variables (such as PYTHONPATH)
-h : print this help message and exit (also --help)
-i : inspect interactively after running script; forces a prompt even
if stdin does not appear to be a terminal; also PYTHONINSPECT=x
-I : isolate Python from the user's environment (implies -E and -s)
-m mod : run library module as a script (terminates option list)
-O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x
-OO : remove doc-strings in addition to the -O optimizations
-q : don't print version and copyright messages on interactive startup
-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE
-S : don't imply 'import site' on initialization
-u : unbuffered binary stdout and stderr, stdin always buffered;
also PYTHONUNBUFFERED=x
see man page for details on internal buffering relating to '-u'
-v : verbose (trace import statements); also PYTHONVERBOSE=x
can be supplied multiple times to increase verbosity
-V : print the Python version number and exit (also --version)
when given twice, print more information about the build
-W arg : warning control; arg is action:message:category:module:lineno
also PYTHONWARNINGS=arg
-x : skip first line of source, allowing use of non-Unix forms of #!cmd
-X opt : set implementation-specific option
file : program read from script file
- : program read from stdin (default; interactive mode if a tty)
arg ...: arguments passed to program in sys.argv[1:]
Other environment variables:
PYTHONSTARTUP: file executed on interactive startup (no default)
PYTHONPATH : ';'-separated list of directories prefixed to the
default module search path. The result is sys.path.
PYTHONHOME : alternate <prefix> directory (or <prefix>;<exec_prefix>).
The default module search path uses <prefix>\lib.
PYTHONCASEOK : ignore case in 'import' statements (Windows).
PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.
PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.
PYTHONHASHSEED: if this variable is set to 'random', a random value is used
to seed the hashes of str, bytes and datetime objects. It can also be
set to an integer in the range [0,4294967295] to get hash values with a
predictable seed.
PYTHONMALLOC: set the Python memory allocators and/or install debug hooks
on Python memory allocators. Use PYTHONMALLOC=debug to install debug
hooks.
如上,就這一句
-c cmd : program passed in as string (terminates option list)
好了,來嘗試做一些標準操作
例如,判斷一個長字串裡每個字元出現了多少次,用列表的count方法
PS C:\Users\Administrator> python -c "var='sdfesdvwetohooodfedsfacbfdaczdfew';varlist=list(var);newlist=list(set(varlist));[print('element is '+em+' count is '+str(varlist.count(em))) for em in newlist]"
element is e count is 4
element is o count is 4
element is s count is 3
element is h count is 1
element is a count is 2
element is z count is 1
element is d count is 6
element is t count is 1
element is w count is 2
element is b count is 1
element is c count is 2
element is f count is 5
element is v count is 1
如果嫌太長,就可以把分號當作一個回車來輸入,如下
PS C:\Users\Administrator> python -c "var='sdfesdvwetohooodfedsfacbfdaczdfew'
>> varlist=list(var)
>> newlist=list(set(varlist))
>> [print('element is '+em+' count is '+str(varlist.count(em))) for em in newlist]"#這個迴圈需要用[]符號圈起來
element is h count is 1
element is s count is 3
element is d count is 6
element is a count is 2
element is z count is 1
element is w count is 2
element is o count is 4
element is f count is 5
element is t count is 1
element is c count is 2
element is v count is 1
element is e count is 4
element is b count is 1
有什麼意義,意義在於每個人的專精不同,然後系統環境又存在差異,多一種思路對於應對特定環境有所幫助。
用Python好處是書寫方便,文件多,模組多。
再舉個例子,目前環境限制如下
在一個python3的容器內,無法新增expect,只能用python的pexpect模組。
對anodest01這臺做一個操作,在vagrant目錄下建立一個檔案。
[vagrant@anodest01 ~]$ uname -n;date
anodest01
Sun Apr 8 09:21:33 UTC 2018
[vagrant@anodest01 ~]$ ls -lt
total 0
執行命令
root@24ebede9724d:/# python -c "import pexpect
> pwd='vagrant'
> process=pexpect.spawn('ssh vagrant@192.168.8.82 touch from_master_docker_py3')
> process.expect('[Pp]assword')
> process.sendline(pwd)
> process.expect(pexpect.EOF)"
在anodest01上看效果,建立完成
[vagrant@anodest01 ~]$ uname -n;date
anodest01
Sun Apr 8 09:23:02 UTC 2018
[vagrant@anodest01 ~]$ ls -lt
total 0
-rw-rw-r--. 1 vagrant vagrant 0 Apr 8 09:22 from_master_docker_py3
以上就是今天研究的例項,但目前來說使用上沒有多複雜,對於複雜的需求是什麼呢
自然是轉譯符的問題,python -c "",預設是使用了雙引號引用,裡面的字元什麼的都要用單引號,如果遇到了需要雙引號的情況,大家還需要在實際場景中多嘗試和努力。
相關文章
- 一行 Python 程式碼Python
- 為什麼我要垂直對齊程式碼(你也要如此!)
- 上次發版我就改了一行程式碼!行程
- 一行 Python 程式碼實現並行Python並行
- python寫程式碼怎麼跳到下一行Python
- 一行 Python 程式碼搞定一棵樹Python
- 程式設計師也要養生程式設計師
- Furion分表分庫我也要happy codingAPP
- Python安裝與第一行程式碼和儲存程式Python行程
- python如何另起一行Python
- 對oracle metalink文件我們也要敢於質疑Oracle
- 怎樣用一行 Python 程式碼實現並行Python並行
- 程式設計師入職三個月,竟一行程式碼都沒提交:我愛程式碼,程式碼使我快樂程式設計師行程
- 怎麼了?我就磕上安卓了!沒有男朋友我也要做安卓安卓
- Python3一行程式碼新增cv2庫Python行程
- 只需一行Python程式碼即可玩20幾款小遊戲Python遊戲
- 人人都能學會的python程式設計教程1:第一行程式碼Python程式設計行程
- 「Python實用祕技08」一行程式碼解析地址資訊Python行程
- 神奇的Python,一行程式碼能做哪些炫酷的事情?Python行程
- Python3一行程式碼搭建極簡web服務Python行程Web
- 我一行程式碼都不寫實現Toolbar!你卻還在封裝BaseActivity?行程封裝
- 教大家python讀取一行一行檔案內容的方法Python
- 一行Python程式碼能實現什麼喪心病狂的功能?Python
- 一行程式碼建立cell行程
- 一行 CSS 程式碼的魅力CSS
- 一行神奇的javascript程式碼JavaScript
- Python:如何用一行程式碼獲取上個月是幾月Python行程
- python合併相同行只保留一行Python
- 一行python生成終端二維碼Python
- 《從零開始學Swift》學習筆記(Day 1)——我的第一行Swift程式碼Swift筆記
- 奧巴馬的第一行程式碼行程
- Python技巧-只用一行程式碼輕鬆實現圖片文字識別Python行程
- 一行程式碼如何隱藏Linux程式?行程Linux
- 我寫的 Python 程式碼,同事都說好Python
- 程式碼優化指南:人生苦短,我用Python優化Python
- 前端也要懂的IOC前端
- 用一行Python進行資料收集探索Python
- “誤入”清華誇誇群?一行Python程式碼幫你寫個機器人Python機器人