Board logo

标题: Python3命令集 [打印本页]

作者: admin    时间: 2021-12-15 11:46     标题: Python3命令集

笺注:这是在 CentOS8编译安装Zabbix4.4.5 的基础上进行的。 操作系统的版本信息: [root@centos8 ~]# cat /etc/redhat-release CentOS Linux release 8.2.2004 (Core) [root@centos8 ~]# [root@centos8 ~]# uname -r 4.18.0-193.el8.x86_64 Python的版本信息: [root@centos8 ~]# python3 --version Python 3.6.8 ###### [root@centos8 ~]# cat xx.py #coding=utf-8 Key_1 = "zhuohua" Key_2 = 18 print(f"Hello,my name is {Key_1}.") print(f"{Key_1}今年{Key_2}岁。") 设置脚本权限: [root@centos8 ~]# chmod a+x xx.py 脚本运行的结果: [root@centos8 ~]# python3 xx.py Hello,my name is zhuohua. zhuohua今年18岁。 [root@centos8 ~]# ###### [root@centos8 ~]# cat xx.py #coding=utf-8 a,b,c = 1,2,'zhuohua' print(a,b,c) print(a) print(b) print(c) 脚本运行的结果: [root@centos8 ~]# python3 xx.py 1 2 zhuohua 1 2 zhuohua [root@centos8 ~]# ###### [root@centos8 ~]# cat xx.py #coding=utf-8 a,b,*c = [1,2,3,4,'zhuohua'] print(a,b,c) print(a) print(b) print("-" * 10) print(c[0]) print(c[1]) print(c[2]) 脚本运行的结果: [root@centos8 ~]# python3 xx.py 1 2 [3, 4, 'zhuohua'] 1 2 ---------- 3 4 zhuohua [root@centos8 ~]# ###### [root@centos8 ~]# cat xx.py #coding=utf-8 list_1 = [1,2,3] list_2 = [4,5,6] list_3 = [7,8,9] Result_1 = [list_1,list_2,list_3] a,b,c = Result_1 print(a) print(b) print(c) print(c[0]) print(c[1]) print(c[2]) 脚本运行的结果: [root@centos8 ~]# python3 xx.py [1, 2, 3] [4, 5, 6] [7, 8, 9] 7 8 9 [root@centos8 ~]# ###### 英文大小写字母的转换 [root@centos8 ~]# cat xx.py #coding=utf-8 Key_1 = '你好,Welcome to zhuohua.' print(Key_1) print("-" * 10) a = Key_1.lower()#英文转小写字母 print(a) a = Key_1.upper()#英文转大写字母 print(a) print("-" * 10) print("-" * 10) fields_1 = ['zhuohua','Python','Mary'] fields_2 = [] fields_3 = [] print(fields_1) print("-" * 10) for Key_1 in fields_1: fields_2.append(Key_1.lower()) print(fields_2) print("-" * 10) for Key_1 in fields_1: fields_3.append(Key_1.upper()) print(fields_3) 脚本运行的结果: [root@centos8 ~]# python3 xx.py 你好,Welcome to zhuohua. ---------- 你好,welcome to zhuohua. 你好,WELCOME TO ZHUOHUA. ---------- ---------- ['zhuohua', 'Python', 'Mary'] ---------- ['zhuohua', 'python', 'mary'] ---------- ['ZHUOHUA', 'PYTHON', 'MARY'] [root@centos8 ~]# ###### 随机显示列表里的某个元素 [root@centos8 ~]# cat xx.py #coding=utf-8 import random Key_1 = random.choice(['zhuohua','Python','Mary',168]) print(Key_1) 多次运行脚本的结果: [root@centos8 ~]# python3 xx.py Mary [root@centos8 ~]# python3 xx.py 168 [root@centos8 ~]# python3 xx.py zhuohua [root@centos8 ~]# python3 xx.py zhuohua [root@centos8 ~]# python3 xx.py Python [root@centos8 ~]# ###### 运行另外一个Shell脚本 [root@centos8 ~]# cat yy.sh #!/bin/bash echo 'Welcome to zhuohua.' [root@centos8 ~]# cat xx.py #coding=utf-8 import os String_1 = ("bash ./yy.sh") Key_1 = os.system(String_1) 脚本 xx.py运行的结果: [root@centos8 ~]# python3 xx.py Welcome to zhuohua. [root@centos8 ~]# ###### 运行另外一个Python脚本 [root@centos8 ~]# cat yy.py #coding=utf-8 def func1(): print('Welcome to zhuohua.') if __name__ == '__main__': func1() [root@centos8 ~]# cat xx.py #coding=utf-8 import os String_1 = ("python3 ./yy.py") Key_1 = os.system(String_1) 脚本 xx.py运行的结果: [root@centos8 ~]# python3 xx.py Welcome to zhuohua. [root@centos8 ~]# ###### 调用Linux命令 [root@centos8 ~]# cat xx.py #coding=utf-8 import os String_1 = "hostname" #显示主机名 Key_1 = os.popen(String_1).read() #返回的字符串的右边会多了个换行符 print(Key_1) 脚本运行的结果: [root@centos8 ~]# python3 xx.py centos8.zhuohua.store [root@centos8 ~]# ###### 调用Linux命令 [root@centos8 ~]# cat xx.py #coding=utf-8 import os String_1 = "systemctl restart httpd" #重启Apache Key_1 = os.popen(String_1).read() print(Key_1) 脚本运行的结果:(Apache的确重启了) [root@centos8 ~]# python3 xx.py [root@centos8 ~]# ###### 调用Linux命令 [root@centos8 ~]# cat xx.py #coding=utf-8 import os String_1 = "php -v" #显示PHP的版本信息 Key_1 = os.popen(String_1).read() print(Key_1) 脚本运行的结果: [root@centos8 ~]# python3 xx.py PHP Warning: Module 'bcmath' already loaded in Unknown on line 0 PHP 7.2.24 (cli) (built: Oct 22 2019 08:28:36) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies [root@centos8 ~]# ###### 调用Linux命令 [root@centos8 ~]# cat xx.py #coding=utf-8 import os String_1 = "php -v > /root/1.txt" #把PHP的版本信息写入(覆盖)到文件/root/1.txt Key_1 = os.popen(String_1).read() print(Key_1) 脚本运行的结果:(文件/root/1.txt不存在时,会自动创建) [root@centos8 ~]# python3 xx.py PHP Warning: Module 'bcmath' already loaded in Unknown on line 0 [root@centos8 ~]# [root@centos8 ~]# cat 1.txt PHP 7.2.24 (cli) (built: Oct 22 2019 08:28:36) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies [root@centos8 ~]# ###### 调用Linux命令 [root@centos8 ~]# cat xx.py #coding=utf-8 import os String_1 = "mysql -V >> /root/2.txt" #把MariaDB的版本信息写入(追加)到文件/root/2.txt Key_1 = os.popen(String_1).read() print(Key_1) 脚本运行的结果:(文件/root/2.txt不存在时,会自动创建) [root@centos8 ~]# python3 xx.py [root@centos8 ~]# [root@centos8 ~]# cat 2.txt mysql Ver 15.1 Distrib 10.3.17-MariaDB, for Linux (x86_64) using readline 5.1 [root@centos8 ~]# ###### 调用Linux命令 [root@centos8 ~]# cat xx.py #coding=utf-8 import os #先创建系统用户,再设置用户密码: String_1 = "adduser zhuohua -s /sbin/nologin && echo 'mima' | passwd --stdin zhuohua" Key_1 = os.popen(String_1).read() print(Key_1) 脚本运行的结果: [root@centos8 ~]# python3 xx.py 更改用户 zhuohua 的密码 。 passwd:所有的身份验证令牌已经成功更新。 [root@centos8 ~]# [root@centos8 ~]# tail -1 /etc/passwd zhuohua:x:1001:1001::/home/zhuohua:/sbin/nologin [root@centos8 ~]# [root@centos8 ~]# tail -1 /etc/shadow zhuohua:$6$m8QlnywNho0mklZK$AZ/ontTfIXYGaBeM91nuTtqrCsDa/LVGpAw6odH8LCICIaR7u6IAwPJ53JYXhlEOlKcWtWqZQMHZRWqWTd9VO.:19035:0:99999:7::: [root@centos8 ~]# ###### 调用Linux命令 [root@centos8 ~]# cat xx.py #coding=utf-8 import os String_1 = "pkill -9 -U zabbix && reboot" #先关闭Zabbix所有相关的进程,再重启服务器 Key_1 = os.popen(String_1).read() print(Key_1) 脚本运行的结果:(服务器的确重启了) [root@centos8 ~]# python3 xx.py [root@centos8 ~]# ############ ############ [root@centos8 ~]# df -hl 文件系统 容量 已用 可用 已用% 挂载点 devtmpfs 883M 0 883M 0% /dev tmpfs 901M 0 901M 0% /dev/shm tmpfs 901M 33M 868M 4% /run tmpfs 901M 0 901M 0% /sys/fs/cgroup /dev/mapper/cl-root 76G 3.5G 73G 5% / /dev/nvme0n1p1 190M 141M 36M 80% /boot tmpfs 181M 0 181M 0% /run/user/0 /dev/sr0 7.7G 7.7G 0 100% /mnt/cdrom [root@centos8 ~]# ###### 调用Linux命令,获取返回字符串中第N行的某个数据 [root@centos8 ~]# cat xx.py #coding=utf-8 import os String_1 = "df -hl | head -7 |tail -1 |awk '{print $4}' |awk -FM '{print $1}'" # head -7 第7行 Key_1 = os.popen(String_1).read() #返回的字符串的右边会多了个换行符 print(Key_1) print("-" * 10) Key_2 = Key_1.rstrip('\n') #去除字符串右边的换行符 print(f"分区/boot的可用量为{Key_2}M") print("-" * 10) print(Key_2) print(type(Key_2)) Key_3 = int(Key_2) #转换为整数 print(Key_3) print(type(Key_3)) 脚本运行的结果: [root@centos8 ~]# python3 xx.py 36 ---------- 分区/boot的可用量为36M ---------- 36 36 [root@centos8 ~]# ###### 调用Linux命令,获取返回字符串中包含关键字的那行的某个数据 [root@centos8 ~]# cat xx.py #coding=utf-8 import os String_1 = "df -hl | grep 'cl-root' | awk '{print $5}' |awk -F% '{print $1}'" Key_1 = os.popen(String_1).read() #返回的字符串的右边会多了个换行符 print(Key_1) print("-" * 10) Key_2 = Key_1.strip('\n') #去除字符串左边和右边的换行符 print(f"根分区的使用率为{Key_2}%") print("-" * 10) print(Key_2) print(type(Key_2)) Key_3 = int(Key_2) #转换为整数 print(Key_3) print(type(Key_3)) 脚本运行的结果: [root@centos8 ~]# python3 xx.py 5 ---------- 根分区的使用率为5% ---------- 5 5 [root@centos8 ~]# ###### 调用Linux命令,获取返回字符串中以关键字结尾的那行的某个数据 [root@centos8 ~]# cat xx.py #coding=utf-8 import os String_1 = "df -hl | grep '/run$' | awk '{print $5}' |awk -F% '{print $1}'" Key_1 = os.popen(String_1).read() #返回的字符串的右边会多了个换行符 print(Key_1) print("-" * 10) Key_2 = Key_1.strip() #去除字符串左边和右边的空格、换行符 print(f"分区/run的使用率为{Key_2}%") print("-" * 10) print(Key_2) print(type(Key_2)) Key_3 = float(Key_2) #转换为浮点数 print(Key_3) print(type(Key_3)) 脚本运行的结果: [root@centos8 ~]# python3 xx.py 4 ---------- 分区/run的使用率为4% ---------- 4 4.0 [root@centos8 ~]# ###### 科学计算(加法) [root@centos8 ~]# cat xx.py #coding=utf-8 Num_1 = 88600 Num_2 = 111_222_333 Num_3 = Num_1 + Num_2 print(Num_1) print(Num_2) print(Num_3) print("-" * 10) print(f"{Num_1:,}") print(f"{Num_2:,}") print(f"{Num_3:,}") print("-" * 10) print(f"运算结果为 {Num_3:,}") 脚本运行的结果: [root@centos8 ~]# python3 xx.py 88600 111222333 111310933 ---------- 88,600 111,222,333 111,310,933 ---------- 运算结果为 111,310,933 [root@centos8 ~]# ###### 科学计算(减法) [root@centos8 ~]# cat xx.py #coding=utf-8 Num_1 = 88600 Num_2 = 111_222_333 Num_3 = Num_1 - Num_2 print(type(Num_1)) print(type(Num_2)) print(type(Num_3)) print("-" * 10) Result_1 = f"{Num_3:,}" print(f"运算结果为 {Result_1}") 脚本运行的结果: [root@centos8 ~]# python3 xx.py ---------- 运算结果为 -111,133,733 [root@centos8 ~]# ###### 科学计算(乘法) [root@centos8 ~]# cat xx.py #coding=utf-8 Num_1 = 88600 Num_2 = 111_222_333 Num_3 = Num_1 * Num_2 print(type(Num_1)) print(type(Num_2)) print(type(Num_3)) print("-" * 10) print(Num_3) Result_1 = f"{Num_3:,}" print(f"运算结果为 {Result_1}") 脚本运行的结果: [root@centos8 ~]# python3 xx.py ---------- 9854298703800 运算结果为 9,854,298,703,800 [root@centos8 ~]# ###### 科学计算(除法) [root@centos8 ~]# cat xx.py #coding=utf-8 Num_1 = 88600 Num_2 = 111 Num_3 = Num_1 / Num_2 print(f"运算结果为 {Num_3}") print(type(Num_3)) print("-" * 10) Result_1 = "%.2f" % Num_3#结果保留两位小数 print(Result_1) print(type(Result_1)) print("-" * 10) Result_2 = int(float(Result_1)) #先转换为浮点数,再转换为整数(直接截取整数部分,不是四舍五入) print(Result_2) print(type(Result_2)) 脚本运行的结果: [root@centos8 ~]# python3 xx.py 运算结果为 798.1981981981982 ---------- 798.20 ---------- 798 [root@centos8 ~]# ###### 科学计算(取余数) [root@centos8 ~]# cat xx.py #coding=utf-8 Key_1 = int(input("请输入任意整数:")) if Key_1 % 2 == 0: print("输入值 " + str(Key_1) + " 是偶数。") else: print("输入值 " + str(Key_1) + " 是奇数。") 多次运行脚本的结果: [root@centos8 ~]# python3 xx.py 请输入任意整数:8 输入值 8 是偶数。 [root@centos8 ~]# [root@centos8 ~]# python3 xx.py 请输入任意整数:-8 输入值 -8 是偶数。 [root@centos8 ~]# [root@centos8 ~]# python3 xx.py 请输入任意整数:9 输入值 9 是奇数。 [root@centos8 ~]# [root@centos8 ~]# python3 xx.py 请输入任意整数:-9 输入值 -9 是奇数。 [root@centos8 ~]# ###### 科学计算(求平方根) [root@centos8 ~]# cat xx.py #coding=utf-8 Key_1 = float(input("请输入任意数字:")) Result_1 = Key_1 ** 0.5 print(f"输入值的平方根为{Result_1}") 多次运行脚本的结果: [root@centos8 ~]# python3 xx.py 请输入任意数字:4 输入值的平方根为2.0 [root@centos8 ~]# [root@centos8 ~]# python3 xx.py 请输入任意数字:16 输入值的平方根为4.0 [root@centos8 ~]# [root@centos8 ~]# python3 xx.py 请输入任意数字:64 输入值的平方根为8.0 [root@centos8 ~]# [root@centos8 ~]# python3 xx.py 请输入任意数字:-64 输入值的平方根为(4.898587196589413e-16+8j) [root@centos8 ~]# [root@centos8 ~]# python3 xx.py 请输入任意数字:8.8 输入值的平方根为2.9664793948382653 [root@centos8 ~]# ###### 科学计算(求立方根) [root@centos8 ~]# cat xx.py #coding=utf-8 import math x = float(input("请输入x的值:")) y = float(input("请输入y的值:")) Result_1 = math.pow(x, y) #pow()函数的作用是返回x的y次方的值 print(f"{x}的{y}次方的值为{Result_1}") print("-" * 10) print("结果直接截取整数部分:") Result_2 = int(Result_1) print(Result_2) 多次运行脚本的结果: [root@centos8 ~]# python3 xx.py 请输入x的值:8 请输入y的值:3 8.0的3.0次方的值为512.0 ---------- 结果直接截取整数部分: 512 [root@centos8 ~]# [root@centos8 ~]# python3 xx.py 请输入x的值:8.5 请输入y的值:3 8.5的3.0次方的值为614.125 ---------- 结果直接截取整数部分: 614 [root@centos8 ~]# ###### 正则表达式 例子一: 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

name">导演: 李大杰

' Result_1 = re.compile(u'[Nn]ame">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

Name">导演: 李大杰

' Result_1 = re.compile(u'[Nn]ame">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

name">导演: 李大杰

' Result_1 = re.compile(u'[Ny]ame">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 脚本运行的结果: [root@centos8 ~]# python3 xx.py [] Traceback (most recent call last): File "xx.py", line 8, in print(Result_2[0]) IndexError: list index out of range [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

name">导演: 李大杰

' Result_1 = re.compile(u'[ny]ame">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

yame">导演: 李大杰

' Result_1 = re.compile(u'[ny]am[e,1]">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

yame">导演: 李大杰

' Result_1 = re.compile(u'[nym]am[,e1]">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

name">导演: 李大杰

' Result_1 = re.compile(u'[b-d]">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 脚本运行的结果: [root@centos8 ~]# python3 xx.py [] Traceback (most recent call last): File "xx.py", line 8, in print(Result_2[0]) IndexError: list index out of range [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

name">导演: 李大杰

' Result_1 = re.compile(u'[^b-d]">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

name">导演: 李大杰

' Result_1 = re.compile(u'[b-e]">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

name">导演: 李大杰

' Result_1 = re.compile(u'[a-z]">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 注释: [a-z] 表示匹配任意一个小写英文字母。 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

Name">导演: 李大杰

' Result_1 = re.compile(u'[A-Z]ame">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 注释: [A-Z] 表示匹配任意一个大写英文字母。 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

Name">导演: 李大杰

' Result_1 = re.compile(u'[A-Za-z]am[A-Za-z]">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 注释: [A-Za-z] 表示匹配任意一个不区分大小写的英文字母。 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

name8">导演: 李大杰

' Result_1 = re.compile(u'[0-7]">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 脚本运行的结果: [root@centos8 ~]# python3 xx.py [] Traceback (most recent call last): File "xx.py", line 8, in print(Result_2[0]) IndexError: list index out of range [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

name8">导演: 李大杰

' Result_1 = re.compile(u'[^0-7]">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

name8">导演: 李大杰

' Result_1 = re.compile(u'[0-8]">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

name82">导演: 李大杰

' Result_1 = re.compile(u'[0-9][0-9]">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 注释: [0-9] 表示匹配任意一个数字。 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

name82">导演: 李大杰

' Result_1 = re.compile(u'[^0-9]ame[0-9][0-9]">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 注释: [^0-9] 表示匹配任意一个非数字字符。 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

name8_">导演: 李大杰

' Result_1 = re.compile(u'[\w][\w][\w]">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 注释: [\w] 表示匹配任意一个单词字符,包括字母、数字、下划线。 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

Name8">导演: 李大杰

' Result_1 = re.compile(u'[\w]am[\w][\w]">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

?am @">导演: 李大杰

' Result_1 = re.compile(u'[\W]am[\W][\W]">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 注释: [\W] 表示匹配任意一个非单词字符,包括空格、@、&、#、%、? 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

Name.。!">导演: 李大杰

' Result_1 = re.compile(u'[\w]ame[\W][\W][\W]">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# 筛选出“导演”: [root@centos8 ~]# cat xx.py #coding=utf-8 import re Key_1 = '

Name8.@&">导演: 李大杰

' Result_1 = re.compile(u'...[\W].">导演: (.*?)

.*?') Result_2 = Result_1.findall(Key_1) print(Result_2) print(type(Result_2)) print(Result_2[0]) [root@centos8 ~]# 注释: . 表示匹配除了换行符之外的任意单个字符。 脚本运行的结果: [root@centos8 ~]# python3 xx.py ['李大杰'] 李大杰 [root@centos8 ~]# ###### Linux使用内置模块ftplib+crontab计划任务 下载FTP根目录里当前日期的log文件到客户端本地的文件夹/share: [root@centos8 ~]# cat xx.py #coding=utf-8 import ftplib import datetime def ftp_download(file_remote,file_local): try: host = '192.168.168.130' username = 'zhuo' password = '168' f = ftplib.FTP(host) f.login(username, password) f.encoding = 'utf-8' pwd_path = f.pwd() print("FTP当前工作目录:", pwd_path) bufsize = 1024 #设置缓冲器大小 fp = open(file_local, 'wb') f.retrbinary('RETR %s' % file_remote, fp.write, bufsize) fp.close() except Exception as e: print(f"远程文件{file_remote}下载失败,原因: {e}") else: print(f"远程文件{file_remote}成功下载到客户端本地的文件夹{Path_1}。") finally: f.quit() if __name__ == '__main__': dt = datetime.datetime.now() tt = dt.strftime('%Y%m%d') print(f"当前的日期:{tt}") Path_1 = "/share" file_remote = f'lnmp-install_{tt}.log' #远程文件 file_local = f'{Path_1}/lnmp-install_{tt}.log' #本地文件 ftp_download(file_remote,file_local) 脚本运行的结果: [root@centos8 ~]# python3 xx.py 当前的日期:20220709 FTP当前工作目录: / 远程文件lnmp-install_20220709.log成功下载到客户端本地的文件夹/share。 [root@centos8 ~]# ### 上传客户端本地的文件夹/share里当前日期的txt文件到FTP子目录/dir_2: [root@centos8 ~]# cat yy.py #coding=utf-8 import ftplib import datetime def ftp_upload(file_remote,file_local): try: host = '192.168.168.130' username = 'zhuo' password = '168' f = ftplib.FTP(host) f.login(username, password) f.encoding = 'utf-8' f.cwd('/dir_2') #切换FTP的工作目录 pwd_path = f.pwd() print("FTP当前工作目录:", pwd_path) bufsize = 1024 fp = open(file_local, 'rb') f.storbinary('STOR ' + file_remote, fp, bufsize) fp.close() except Exception as e: print(f"本地文件{file_local}上传失败,原因: {e}") else: print(f"本地文件{file_local}上传成功。") finally: f.quit() if __name__ == '__main__': dt = datetime.datetime.now() tt = dt.strftime('%Y%m%d') print(f"当前的日期:{tt}") file_remote = f'Install_{tt}.txt' #远程文件 file_local = f'/share/Install_{tt}.txt' #本地文件 ftp_upload(file_remote,file_local) 脚本运行的结果: [root@centos8 ~]# python3 yy.py 当前的日期:20220709 FTP当前工作目录: /dir_2 本地文件/share/Install_20220709.txt上传成功。 [root@centos8 ~]# ### 使用crontab计划任务在每天的11:05自动运行以下两个Python脚本: [root@centos8 ~]# crontab -e 追加: 05 11 * * * /usr/local/bin/python3 /root/xx.py 05 11 * * * /usr/local/bin/python3 /root/yy.py 查看当前用户的crontab计划任务: [root@centos8 ~]# crontab -l 05 11 * * * /usr/local/bin/python3 /root/xx.py 05 11 * * * /usr/local/bin/python3 /root/yy.py [root@centos8 ~]# ###### Linux使用内置模块ftplib+正则表达式 下载FTP根目录里当前日期的log文件到客户端本地的文件夹/share: [root@centos8 ~]# cat xx.py #coding=utf-8 import ftplib import datetime import re def ftp_download(): host = '192.168.168.130' username = 'zhuo' password = '168' f = ftplib.FTP(host) f.login(username, password) f.encoding = 'utf-8' f.cwd('/') #切换FTP的工作目录 pwd_path = f.pwd() print("FTP当前工作目录:", pwd_path) print("-" * 10) print("当前FTP工作目录下所有的log文件:") K_1 = '.log' #文件后缀 for file in f.nlst(): if file.endswith(K_1): print(file) print("-" * 10) for file in f.nlst(): if file.endswith(K_1): Result_1 = re.compile(u'lnmp-install_(.*?)[\w][\w][\w][\w][\w][\w].log.*?') Result_2 = Result_1.findall(file) #print(Result_2) #print(Result_2[0]) if Result_2[0] == tt: file_local = f"/share/{file}" file_remote = file bufsize = 1024 fp = open(file_local, 'wb') f.retrbinary('RETR %s' % file_remote, fp.write, bufsize) fp.close() print(f"远程文件{file_remote}下载成功。") f.quit() if __name__ == '__main__': dt = datetime.datetime.now() tt = dt.strftime('%Y%m%d') print(f"当前的日期:{tt}") ftp_download() 脚本运行的结果: [root@centos8 ~]# python3 xx.py 当前的日期:20220712 FTP当前工作目录: / ---------- 当前FTP工作目录下所有的log文件: lnmp-install_20211215.log lnmp-install_20220709.log lnmp-install_20220710160723.log lnmp-install_20220712160723.log lnmp-install_20220712180788.log ---------- 远程文件lnmp-install_20220712160723.log下载成功。 远程文件lnmp-install_20220712180788.log下载成功。 [root@centos8 ~]# ### 上传客户端本地的文件夹/share里当前日期的txt文件到FTP子目录/dir_2: [root@centos8 ~]# cat yy.py
  1. #coding=utf-8
  2. import ftplib
  3. import datetime
  4. import os,re
  5. def ftp_upload(file_remote,file_local):
  6. host = '192.168.168.130'
  7. username = 'zhuo'
  8. password = '168'
  9. f = ftplib.FTP(host)
  10. f.login(username, password)
  11. f.encoding = 'utf-8'
  12. f.cwd('/dir_2') #切换FTP的工作目录,这是切换到FTP的子目录
  13. pwd_path = f.pwd()
  14. print("FTP当前工作目录:", pwd_path)
  15. bufsize = 1024
  16. fp = open(file_local, 'rb')
  17. f.storbinary('STOR ' + file_remote, fp, bufsize)
  18. fp.close()
  19. f.quit()
  20. def func1():
  21. dir_1 = "/share/" #客户端本地文件夹
  22. K_1 = '.txt' #文件后缀
  23. os.chdir(dir_1) #切换客户端的工作目录,以防找不到本地文件
  24. print(f"客户端当前的工作目录{dir_1}下所有的文件、目录:")
  25. a = os.listdir() #显示客户端当前的工作目录里的文件、目录
  26. print(a)
  27. print("-" * 10)
  28. print("-" * 10)
  29. f = list(os.listdir(dir_1))
  30. for i in range(len(f)):
  31. if f[i].endswith(K_1):
  32. Result_1 = re.compile(u'lnmp-install_(.*?)[\w][\w][\w][\d][\d][0-9].txt.*?')
  33. Result_2 = Result_1.findall(f[i])
  34. #print(f[i])
  35. #print(Result_2)
  36. #print(Result_2[0])
  37. if Result_2[0] == tt:
  38. ftp_upload(f[i],f[i])
  39. print(f"本地文件{f[i]}上传成功。")
  40. if __name__ == '__main__':
  41. dt = datetime.datetime.now()
  42. tt = dt.strftime('%Y%m%d')
  43. print(f"当前的日期:{tt}")
  44. func1()
复制代码
脚本运行的结果: [root@centos8 ~]# python3 yy.py 当前的日期:20220712 客户端当前的工作目录/share/下所有的文件、目录: ['mydir', 'lnmp-install_20220712.txt', 'lnmp-install_20220712.log', 'lnmp-install_20220711160728.txt', 'lnmp-install_20220712170718.txt', 'lnmp-install_20220712060723.txt'] ---------- ---------- FTP当前工作目录: /dir_2 本地文件lnmp-install_20220712170718.txt上传成功。 FTP当前工作目录: /dir_2 本地文件lnmp-install_20220712060723.txt上传成功。 [root@centos8 ~]# 相关文章: 正则表达式 全局变量和局部变量 字符串的截取 自定义函数 匿名函数 Python3脚本管理Access Linux使用内置模块ftplib CentOS6使用tcping+crontab计划任务 CentOS8使用计划任务 Python3调用Linux命令 Python3调用另外一个脚本的函数/类




欢迎光临 blog.zhuohua.store (http://blog.zhuohua.store/) Powered by Discuz! 7.2