Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3650987
  • 博文数量: 880
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 6155
  • 用 户 组: 普通用户
  • 注册时间: 2016-11-11 09:12
个人简介

To be a better coder

文章分类

全部博文(880)

文章存档

2022年(5)

2021年(60)

2020年(175)

2019年(207)

2018年(210)

2017年(142)

2016年(81)

分类: LINUX

2019-11-25 11:22:00

111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
#!/usr/bin/python3采用了绝对路径的写法,即指定了采用/usr/bin/python3该路径下的解释器来执行脚本。如果python3解释器不在该路径下的话(用anaconda安装的话有可能不在),./script.py 就无法运行。
而 #!/usr/bin/env python3 的写法指定从PATH环境变量中查找python解释器的位置,因此只要环境变量中存在,该脚本即可执行。所以一般情况下采用 #!/usr/bin/env python3 的写法更好,容错率更高。
2222222222222222222222222222222222222222222222222222222222222222222222222222222
文件的读写操作
import os
def read_file_info(file):
    if os.access(file, os.R_OK) == False:
        raise Exception ----------------------------------------------------抛出异常
    fd = os.open(file, os.O_RDONLY)
    ret = os.read(fd, 600)
    os.close(fd)
    return ret

def write_file_info(file, date):
    fd = os.open(file, os.O_RDWR|os.O_CREAT)
    ret = os.write(fd, date)
    os.close(fd)
    return 0
3333333333333333333333333333333333333333333333333333333333333333333333333333333
debug打印信息
import sys
import time
def debug(string):
    try:
        raise Exception
    except:
        f = sys.exc_info()[2].tb_frame.f_back
    date = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
    file = f.f_code.co_filename[f.f_code.co_filename.rfind('/') + 1:]
    func = f.f_code.co_name
    line = f.f_lineno
    print('\033[1;31;40m%s, %s, %s, %d \033[0m: %s ' % (date, file, func, line, string))
    return
444444444444444444444444444444444444444444444444444444444444444444444444444444
getopt传参
import getopt
import sys

opts,args = getopt.getopt(sys.argv[1:],'-h-f:-v',['help','filename=','version'])
print(opts)
for opt_name,opt_value in opts:
    if opt_name in ('-h','--help'):
        print("[*] Help info")
        sys.exit()
    if opt_name in ('-v','--version'):
        print("[*] Version is 0.01 ")
        sys.exit()
    if opt_name in ('-f','--filename'):
        fileName = opt_value
        print("[*] Filename is ",fileName)
        # do something
        sys.exit()


阅读(1785) | 评论(0) | 转发(0) |
0

上一篇:chrony配置同步

下一篇:几个漏洞

给主人留下些什么吧!~~