随笔分类 -  python-linux

python
摘要:if os.system('lss') !=0: print 'Without the command' 阅读全文
posted @ 2019-03-02 09:36 effortsing 阅读(5356) 评论(0) 推荐(0) 编辑
摘要:pip安装软件报错 utf-8 code can't decode byte 0xcf in position7 根据错误提示的路径找到__init__.py文件 根据错误提示的最后几句话找到对应的行数,将’utf-8’修改为’gbk’即可,如下所示if sys.version_info >= (3,): def console_to_str(s): try: ... 阅读全文
posted @ 2019-02-18 22:49 effortsing 阅读(761) 评论(0) 推荐(0) 编辑
摘要:说明:通过jinja2渲染后只能打印出来效果,目前无法保存例1:渲染 .j2 文件 1、安装jinja2模块 pip3 install jinja2 2、定义模板 说明:变量必须是小写,大写有的情况无法替换,亲测;模板必须是j2结尾或者jinja2默认支持的html格式 本实例的文档是不全的,只是截取了其中一段作为例子进行演示 本实例模板中定义了两个需要被渲染的变量 {{ clu... 阅读全文
posted @ 2019-02-08 13:43 effortsing 阅读(1364) 评论(0) 推荐(0) 编辑
摘要:#!/usr/bin/python #-*- codinig: UTF-8 -*- from __future__ import print_function import os, sys, stat import shutil import tarfile import subprocess output=subprocess.check_output(["hostname"], shel... 阅读全文
posted @ 2019-02-04 10:11 effortsing 阅读(401) 评论(0) 推荐(1) 编辑
摘要:直接看下面实例:In [52]: output=subprocess.check_output(["head -c 16 /dev/urandom | od -An -t x | tr -d ' '"], shell=True) In [53]: print(output) ... 阅读全文
posted @ 2019-02-01 17:20 effortsing 阅读(4061) 评论(0) 推荐(0) 编辑
摘要:1、用Python Shell设置或获取环境变量的方法: 设置系统环境变量 os.environ['环境变量名称']='环境变量值' #其中key和value均为string类型 os.putenv('环境变量名称', '环境变量值') 获取系统环境变量 os.environ['环境变量名称'] os.getenv('环境变量名称') 实例一、 In [52]: output... 阅读全文
posted @ 2019-02-01 16:22 effortsing 阅读(4138) 评论(0) 推荐(1) 编辑
摘要:subprocess.call 是不能作为赋值的,需要用到 subprocess.check_output 函数,而且如果要引用赋值就必须使用subprocess.call(['echo',line])这种形式。 In [42]: import subprocess In [101]: subprocess.call(['kubectl','get','nodes']) ... 阅读全文
posted @ 2019-02-01 16:21 effortsing 阅读(357) 评论(0) 推荐(0) 编辑
摘要:1、subprocess.call 里面的命令分开写,实例如下:subprocess.call 是不能作为赋值的,需要用到 subprocess.check_output 函数,而且如果要引用赋值就必须使用subprocess.call(['echo',line])这种形式。 In [42]: import subprocess In [101]: subprocess.call(['kub... 阅读全文
posted @ 2019-01-31 17:46 effortsing 阅读(5048) 评论(0) 推荐(0) 编辑
摘要:strip作用:去掉空格、以及想要去掉的字符,实例如下: In [42]: import subprocess In [42]: output=subprocess.check_output(["kubectl get no | grep test2 | awk '{print $1}'"], shell=True) In [4... 阅读全文
posted @ 2019-01-31 16:35 effortsing 阅读(174) 评论(0) 推荐(0) 编辑
摘要:split作用:把字符串变成列表,这个字符串必须是多行文字。如果是单行文字或一个单词是不行的,实例操作如下: In [46]: output=subprocess.check_output(['df','-h']) In [60]: print(output) ... 阅读全文
posted @ 2019-01-31 16:33 effortsing 阅读(227) 评论(0) 推荐(0) 编辑
摘要:subprocess.call 是不能作为赋值的,需要用到 subprocess.check_output 函数,而且如果要引用赋值就必须使用subprocess.call(['echo',line])这种形式。 实例一、 对于纯字符串操作如下: In [42]: import subprocess In [101]: subprocess.call(['kubectl','get','... 阅读全文
posted @ 2019-01-31 15:53 effortsing 阅读(8473) 评论(0) 推荐(0) 编辑
摘要:Paramiko模块使用 实现目的:192.168.0.61通过Paramiko模块登录192.168.0.63 一、下载安装 由于 paramiko 模块内部依赖pycrypto,所以先下载安装pycrypto pip install pycrypto pip install paramiko 二、模块使用 1、SSHClient 用于连接远程服务器并执行基本命令 (1)基于... 阅读全文
posted @ 2019-01-31 08:22 effortsing 阅读(403) 评论(0) 推荐(0) 编辑
摘要:1、用shutil移动文件,import shutil shutil.move('/root/test.yaml','/home/') shutil.move('/root... 阅读全文
posted @ 2019-01-30 20:01 effortsing 阅读(217) 评论(0) 推荐(0) 编辑
摘要:默认解压到当前目录 import shutil shutil.unpack_archive('/root/redis-3.2.3.tar.gz') 解压到home目录下 import shutil shutil.unpack_archive('/root/redis-3.2.3.tar.gz','/home/') 阅读全文
posted @ 2019-01-30 17:50 effortsing 阅读(184) 评论(0) 推荐(0) 编辑
摘要:1、准备hosts模板 mkdir -p /k8s/profile cat >/k8s/profile/hosts hostname.py <<EOF #!/usr/bin/python #-*- codinig: UTF-8 -*- from __future__ import print_function import os import shutil import tarfile i... 阅读全文
posted @ 2019-01-30 16:27 effortsing 阅读(651) 评论(0) 推荐(0) 编辑
摘要:[root@master ~]# cat a.py #!/usr/bin/python # -*- coding:UTF-8 -*- import subprocess def fun(): subprocess.call(["touch /rubbish/test1"], shell=True) subprocess.call(["touch /rubbish/test2"... 阅读全文
posted @ 2019-01-24 09:11 effortsing 阅读(1992) 评论(0) 推荐(0) 编辑
摘要:提前把mongodb-linux-x86_64-rhel70-3.2.4.tgz放到和脚本相同目录下,然后把下复制到脚本里面,开始执行 #!/usr/bin/python #-*- codinig: UTF-8 -*- from __future__ import print_function import os import shutil import tarfile import subp... 阅读全文
posted @ 2019-01-17 11:29 effortsing 阅读(396) 评论(0) 推荐(0) 编辑
摘要:关键字参数必须跟随在位置参数后面! 因为python函数在解析参数时, 是按照顺序来的, 位置参数是必须先满足, 才能考虑其他可变参数.,否则报错如下: In [74]: print(s1.format(name='jenkins',"good","kg",a='python')) File "<i 阅读全文
posted @ 2018-12-18 11:02 effortsing 阅读(4747) 评论(0) 推荐(0) 编辑
摘要:创建一个脚本,内容如下 [root@bogon ~]# cat a.py #conding:utf-8import sysprint(sys.argv[0]) # 打印sys.argv的第0个参数 执行脚本带上参数[root@bogon ~]# python a.py a b c d ea.py s 阅读全文
posted @ 2018-12-16 14:21 effortsing 阅读(153) 评论(0) 推荐(0) 编辑
摘要:1、遍历字典 dict={'a': '1', 'b': '2', 'c': '3'} for key in dict: print(key+':'+dict[key]) ssh://root@192.168.0.204:22/usr/bin/python -u /home/progect/app/py_code/test1.py a:1 b:2 c:3 Process finis... 阅读全文
posted @ 2018-12-09 20:57 effortsing 阅读(215) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示