思路这种东西,对于非科班出身的我来说,太重要了
因为我不会捧着厚厚的一本书,例如《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 "",默认是使用了双引号引用,里面的字符什么的都要用单引号,如果遇到了需要双引号的情况,大家还需要在实际场景中多尝试和努力。
网友评论