python常用函数与常见问题汇总

pyc文件介绍:
py 文件在执行的时候会先被编译成 PyCodeObject 对象,并且该对象还会被保存到 pyc 文件中

1. 读取yml文件

//获取配置文件路径
config_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.yml")
if os.path.isfile(config_file_path):
    //读取配置
    with open(config_file_path, "r") as f:
        config = yaml.load(f, Loader=yaml.FullLoader)
    else:
        config = {}

2. argparser模块
argparse 模块提供轻松编写用户友好的命令行接口。
argparse 模块会自动生成帮助和使用手册,并在用户给程序传入无效参数时报出错误信息。

实例化ArgumentParser

parser = argparse.ArgumentParser(description = 'test')

使用add_argument函数添加参数

解析参数:

args = parser.parse_args()

3.subprocess.getoutput(cmd)

接收字符串格式的命令,执行命令并返回执行结果,其功能类似于os.popen(cmd).read()和commands.getoutput(cmd)

subprocess模块是官方推荐使用的一个系统函数调用模块,而getoutput方法则是模仿真实的Shell

3.python 接收命令行参数

import sys
sys.argv   返回一个参数列表
  1. paramiko文件夹远程上传
class Dossh:
    def __init__(self,ip,port,uname,passwd):
        self.ip =ip
        self.port =port
        self.uname = uname
        self.passwd = passwd
        self.sshclt = paramiko.SSHClient()
        self.sshclt.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.sshclt.connect(hostname=self.ip, port=self.port, username=self.uname, password=self.passwd,
                       allow_agent=False, look_for_keys=False)
        self.t = paramiko.Transport((self.ip, self.port))
        self.t.connect(username=self.uname, password=self.passwd)
        self.sftp = paramiko.SFTPClient.from_transport(self.t)
    def getssh(self):
        return self.sshclt
    def close_ssh(self):
        self.sshclt.close()
        self.sftp.close()
    def uploadfile_path(self,local_path,remote_path):
        """
        :param local_path:待上传文件夹路径
        :param remote_path:远程路径
        :return:
        """
        local_pathname = os.path.split(local_path)[-1]
        #上传远程后的目录名
        real_remote_Path = remote_path+'/'+local_pathname
        try:
            self.sftp.stat(remote_path)
        except Exception as e:
            self.sshclt.exec_command("mkdir -p %s"%remote_path)
        self.sshclt.exec_command("mkdir -p %s"%real_remote_Path)
        all_files = get_all_files_in_local_dir(local_path)
        for file_path in all_files:
            file_path = file_path.replace("\\", "/")
            off_path_name = file_path.split(local_pathname)[-1]
            abs_path = os.path.split(off_path_name)[0]
            reward_remote_path =real_remote_Path+abs_path
            try:
                self.sftp.stat(reward_remote_path)
            except Exception as e:
                self.sshclt.exec_command("mkdir -p %s" % reward_remote_path)
            abs_file =  os.path.split(file_path)[1]
            to_remote = reward_remote_path+'/'+abs_file
            self.sftp.put(file_path,to_remote)
            print(file_path, to_remote)
  1. request请求 带cookie
post(url, data=params, cookies=cookie, headers=headers)
  1. 安装包问题
    问题1 Command “python setup.py egg_info” failed with error code 1 in /tmp/pip-build-fH0Feg/pip/
解决办法: 更新pip

问题2 pip升级时报错:AttributeError: ‘NoneType‘ object has no attribute ‘bytes‘

解决办法: easy_install -U pip
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值