return
执行到return语句时,会退出函数,return之后的语句不再执行。。。但将return语句放在try语句块中,是个例外。。。。
def fun():
print(kk)
return 'ok'#执行到该return语句时,函数终止,后边的语句不再执行
print(kk)
def func():
try:
print(kk)
return 'ok' #函数得到了一个返回值
finally:#finally语句块中的语句依然会执行
print(kk)
print fun()
print '----------'
print func()
运行结果:
kk
ok
----------
kk
kk
ok
判断列表为空
直接 if a 就是判断在列表不为空的时候,需要执行的命令
即使用’in’之前要保证record不是’None’
a = []
if a:
COMMAND
TypeError: argument of type ‘NoneType’ is not iterable
if casess.metric in endpoint_all_ok_metric_list:
pass
else:
endpoint_all_ok_metric_list.append(casess.metric)
#报错,TypeError: argument of type ‘NoneType’ is not iterable
是因为 用’in’之前要保证list不是’None’
所以,修改了下。
if endpoint_all_ok_metric_list:
if casess.metric in endpoint_all_ok_metric_list:
pass
else:
endpoint_all_ok_metric_list.append(casess.metric)
else:
endpoint_all_ok_metric_list.append(casess.metric)
str去除两边空格
str.strip()
Linux 如何显示一个文件的某几行
- 从第1000行开始,显示2000行。即显示1000~2999行
cat input_file | tail -n +1000 | head -n 2000
tail -n 4000 /home/work/open-falcon/agent/logs/agent.log |grep gpu.alive|head
## 直接tail -n 140000 这样数量太大。 先用cat ,在tail就会好一些.
root@hypereal-System-Product-Name:/home/hypereal# cat /home/work/open-falcon/agent/logs/agent.log |tail -n 13000|wc -l
13000
查看时间间隔内的 日志。
cat /home/work/open-falcon/agent/logs/agent.log|sed -n '/2019\/01\/09 08*/,/2019\/01\/09 09*/p'|grep gpu.alive|grep -v "Value:17"
xxx@xxx:~$ cat /home/work/open-falcon/agent/logs/agent.log|sed -n '/2019\/01\/09 08*/,/2019\/01\/09 09*/p'|grep gpu.alive|grep -v "Value:17"
2019年 01月 09日 星期三 08:17:54 CST:2019/01/09 08:17:54 var.go:88: => <Total=1> <Endpoint:dm/70-85-c2-84-53-a6, Metric:gpu.alive, Type:GAUGE, Tags:cluster=detection-machine,cluster=detection-machine, Step:60, Time:1546993075, Value:3>
2019年 01月 09日 星期三 08:18:55 CST:2019/01/09 08:18:55 var.go:88: => <Total=1> <Endpoint:dm/70-85-c2-84-53-a6, Metric:gpu.alive, Type:GAUGE, Tags:cluster=detection-machine,cluster=detection-machine, Step:60, Time:1546993135, Value:3>
2019年 01月 09日 星期三 08:19:55 CST:2019/01/09 08:19:55 var.go:88: => <Total=1> <Endpoint:dm/70-85-c2-84-53-a6, Metric:gpu.alive, Type:GAUGE, Tags:cluster=detection-machine,cluster=detection-machine, Step:60, Time:1546993195, Value:3>
2019年 01月 09日 星期三 08:53:33 CST:2019/01/09 08:53:33 var.go:88: => <Total=1> <Endpoint:dm/70-85-c2-84-53-a6, Metric:gpu.alive, Type:GAUGE, Tags:cluster=detection-machine,cluster=detection-machine, Step:60, Time:1546995214, Value:3>
2019年 01月 09日 星期三 08:54:33 CST:2019/01/09 08:54:33 var.go:88: => <Total=1> <Endpoint:dm/70-85-c2-84-53-a6, Metric:gpu.alive, Type:GAUGE, Tags:cluster=detection-machine,cluster=detection-machine, Step:60, Time:1546995274, Value:3>
2019年 01月 09日 星期三 08:55:34 CST:2019/01/09 08:55:34 var.go:88: => <Total=1> <Endpoint:dm/70-85-c2-84-53-a6, Metric:gpu.alive, Type:GAUGE, Tags:cluster=detection-machine,cluster=detection-machine, Step:60, Time:1546995334, Value:3>
xxx@xxx:~$
判断是否 数字,字母,
str_1 = "123"
str_2 = "Abc"
str_3 = "123Abc"
#用isdigit函数判断是否数字
print(str_1.isdigit())
Ture
print(str_2.isdigit())
False
print(str_3.isdigit())
False
#用isalpha判断是否字母
print(str_1.isalpha())
False
print(str_2.isalpha())
Ture
print(str_3.isalpha())
False
#isalnum判断是否数字和字母的组合
print(str_1.isalnum())
Ture
print(str_2.isalnum())
Ture
print(str_1.isalnum())
Ture
注意:如果字符串中含有除了字母或者数字之外的字符,比如空格,也会返回False
一个修改terminal 名称的脚本
import requests
from misc import *
import netifaces
import subprocess
import os
import shutil
if os.path.exists('/etc/url'):
with open("/etc/url","r") as env:
for line in env:
if 'WEB_SERVER' in line:
server_url=line.replace("WEB_SERVER=","").strip()
else:
server_url="https://www.xxx.com"
else:
server_url="https://www.xxx.com"
machineId = getMachineId() or "ErrorUnknown"
r = requests.get(server_url + "/Alias/{}".format(machineId))
alias = r.json()['alias']
for interface in sorted(netifaces.interfaces()):
if interface.startswith("en") or interface.startswith("eth"):
mac = netifaces.ifaddresses(interface)[netifaces.AF_LINK][0]['addr']
mac = mac.replace(":","-")
alias=alias +'(' + mac + ')'
if os.path.exists('/root/.bashrc-bak'):
pass
else:
shutil.copyfile("/root/.bashrc","/root/.bashrc-bak") ### copy 文件,
if os.path.exists('/home/hypereal/.bashrc-bak'): ###判断文件
pass
else:
shutil.copyfile("/home/hypereal/.bashrc","/home/hypereal/.bashrc-bak")
shutil.copyfile("/root/.bashrc-bak","/root/.bashrc") ###复制文件
shutil.copyfile("/home/hypereal/.bashrc-bak","/home/hypereal/.bashrc")
cmd_root = "sed -i '/PS1=/ s/u@\\\h/u@"+alias+"/g' /root/.bashrc" ####sed中'\‘的匹配。 \\\ \
cmd_hypereal = "sed -i '/PS1=/ s/u@\\\h/u@"+alias+"/g' /home/hypereal/.bashrc" ### \\\\$alias \$alias
#cmd_root = "sed -i '/PS1=/ s/u@" + alias +"/u@\\\h/g' /root/.bashrc"
#cmd_hypereal = "sed -i '/PS1=/ s/u@"+ alias +"/u@\\\h/g' /home/hypereal/.bashrc"
status,tem = subprocess.getstatusoutput(cmd_root)
status,tem = subprocess.getstatusoutput(cmd_hypereal)
status,tem = subprocess.getstatusoutput("source /root/.bashrc")
status,tem = subprocess.getstatusoutput("source /home/hypereal/.bashrc")