判断一个列表

for h in host:
    try:
        ip.append(obj.get(type,h))
    except:
        ip=[]
        ip.append(obj.get(type,h))
print(ip)

查看当前文件名字
import os
print os.path.basename(__file__)

 

 

range 函数  补齐位数的方法

def fun(x):
    out = []
    for i in range(x):
        if i < 10:
            res = "0{}".format(i)
            out.append(res)
        else:
            res = "{}".format(i)
            out.append(res)

    return out
res=fun(31)

第二种方式 zfill 函数 来实现字符补全长度

for i in range(31):
#因为range 出来的是整数,需要转成字符串 a
=str(i).zfill(2) print(a)

 

 

系统执行命令 进行取值

 

第一种方法(win linux 都可以)
import os
p=os.popen("dir")
x=p.read()

print(x)

第二种方法 只有linux可以
import subprocess

res = subprocess.Popen('dir', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
result = res.stdout.readlines()

 

相关文章:

  • 2022-02-06
  • 2022-12-23
  • 2021-09-16
  • 2022-03-01
  • 2021-07-20
  • 2022-12-23
  • 2022-12-23
  • 2021-09-27
猜你喜欢
  • 2021-05-02
  • 2021-11-27
  • 2022-02-26
  • 2021-06-22
  • 2021-06-27
  • 2022-01-19
  • 2021-11-08
相关资源
相似解决方案