Python常用脚本

1、图片转base64

# !/usr/local/python3.8/bin/python3
# -*- coding:UTF-8 -*-

import os
import base64

# 图片文件夹是/root/images
log_d = '/root/images'
logFiles = os.listdir(log_d)
# 只转图片格式
suffix = ("png", "jpg", "jpeg")
# 在/root/images内遍历文件
for filename in logFiles:
    print(filename)
    bool_file = filename.endswith(suffix)
    if bool_file:
        with open(filename, 'rb') as f:
            # image_base64 = base64.b64encode(f.read())  #bytes类型
            image = f.read()
            image_base64 = str(base64.b64encode(image), encoding='utf-8')  # 字符串类型
            print(image_base64)
    else:
        print('文件格式不匹配:{}'.format(filename))

2、解压zip文件

#!/usr/local/python3.8/bin/python3
# -*- coding:UTF-8 -*-

import zipfile, os
from werkzeug.utils import secure_filename

'''
基本格式:zipfile.ZipFile(filename[,mode[,compression[,allowZip64]]])
mode:可选 r,w,a 代表不同的打开文件的方式;r 只读;w 重写;a 添加
compression:指出这个 zipfile 用什么压缩方法,默认是 ZIP_STORED,另一种选择是 ZIP_DEFLATED;
allowZip64:bool型变量,当设置为True时可以创建大于 2G 的 zip 文件,默认值 True;

'''

def unzip(path, folder_abs):
    zip_file = zipfile.ZipFile(path)
    zip_list = zip_file.namelist()  # 得到压缩包里所有文件

    for f in zip_list:
        zip_file.extract(f, folder_abs)  # 循环解压文件到指定目录
    zip_file.close()  # 关闭文件,必须有,释放内存
    os.remove(path)

def savefile(f, savepath):
    basepath = os.path.dirname(__file__)
    upload_path = os.path.join(basepath, savepath, secure_filename(f.filename))
    f.save(upload_path)
    unzip(upload_path, savepath)

 3、打包文件zipfile,tarfile

# !/usr/bin/env python
# -*- coding:utf-8 -*-


# pip install zipfile38,tarfile不需要安装直接导入
import tarfile, zipfile


def zipDir(dirpath, outFullName):  # dirpath:是要打包的目录, outFullName:是压缩包名字
    zip = zipfile.ZipFile(outFullName, "w", zipfile.ZIP_DEFLATED)
    for path, dirnames, filenames in os.walk(dirpath):
        fpath = path.replace(dirpath, '')
        for filename in filenames:
            zip.write(os.path.join(path, filename), os.path.join(fpath, filename))
    zip.close()


def compress_file(dirpath, tarfilename):  # tarfilename是压缩包名字,dirname是要打包的目录
    if os.path.isfile(dirpath):
        with tarfile.open(tarfilename, 'w') as tar:
            tar.add(dirpath)
    else:
        with tarfile.open(tarfilename, 'w') as tar:
            #记录当前工作目录
            cur_path = os.getcwd()
            #切换工作目录
            os.chdir(dirpath)
            for root, dirs, files in os.walk('.'):
                for single_file in files:
                    # if single_file != tarfilename:
                    filepath = os.path.join(root, single_file)
                    tar.add(filepath)
            #切换回原来工作目录
            os.chdir(cur_path)


if __name__ == '__main__':
    zipDir('D:\\game\\build', 'abc.zip')
    compress_file('test.txt', 'test.tar.gz')
    compress_file('/tmp/test', 'test.tar.gz')

 4、python逐行读取文件

#!/usr/bin/python3.8
# -*- coding:UTF-8 -*-

# 使用fileinput模块
import fileinput
for line in fileinput.input("test.txt"):
    print(line, end='')

# 对一个文件对象使用for循环读每行数据
with open('test.txt','r',encoding='utf-8') as f:
    for line in f:
        print(line, end='')

# 使用readline()函数
with open('test.txt','r',encoding='utf-8') as f:
    line = f.readline()
    while line:
        print(line, end='')  # 在 Python 3中使用
        line = f.readline()

# 一次读取多行数据(f.readlines(2):一次读取两行数据)
with open('test.txt','r',encoding='utf-8') as f:
    tag=True
    while tag:
        lines = f.readlines(2)
        if lines:
            for line in lines:
                print(line, end='')
        else:
            tag=False 

5、读取文件指定行或最后几行

#!/usr/bin/python3.8
# -*- coding:UTF-8 -*-

import linecache


# 放入缓存防止内存过载
def get_line_count(filename):
    """
    :param filename: 文件名
    :return: 返回文件总的行数
    """
    with open(filename, 'r', encoding="utf-8") as f:
        count = 1
        while True:
            buffer = f.read(1024 * 1)
            if not buffer:
                break
            count += buffer.count('\n')
    return count


# 读取文件最后几行,方式一:使用seek()方法
def readfileline(lines, filepath, off=-50):
    """
    file.seek(offset[, whence])
    file:表示文件对象;
    whence:作为可选参数,用于指定文件指针要放置的位置,该参数的参数值有 3 个选择:0 代表文件头(默认值)、1 代表当前位置、2 代表文件尾。
    offset:表示相对于 whence 位置文件指针的偏移量,正数表示向后偏移,负数表示向前偏移。
            例如:
                当whence == 0 && offset == 3(即seek(3,0)),表示文件指针移动至距离文件开头处 3 个字符的位置;
                当whence == 1 && offset == 5(即seek(5,1)),表示文件指针向后移动,移动至距离当前位置5个字符处;
                当whence == 2 && offset == -5(即seek(-5,2)),表示文件指针向前移动,从文件末尾向前移动5个字符。
    :param lines: 要读取的行数
    :param filepath: 文件路径
    :param off: 读取的字符数
    :return:
    """
    linenumber = get_line_count(filepath)
    with open(filepath, 'rb') as f:
        while True:
            f.seek(off, 2)
            res = f.readlines()
            if len(res) > lines:
                for i in range(1, len(res)):
                    print(linenumber - lines + i, res[i].decode(), end="")
                break
            off += -50


# 读取文件最后几行,方式二:使用linecache模块
def readline(lines, filepath):
    linecache.clearcache()
    line_count = get_line_count(filepath)
    line_count = line_count - (lines - 1)
    for i in range(lines):
        last_line = linecache.getline(filepath, line_count)
        print(line_count, last_line, end="")
        line_count += 1


def readfile(start, end, filepath):
    """
    :param start: 开始行
    :param end: 结束行
    :param filepath: 文件路径
    :return:
    """
    linecache.clearcache()
    for i in range(start, end + 1):
        last_line = linecache.getline(filepath, i)
        print(i, last_line, end="")


if __name__ == '__main__':
    # 读取第10行到第20行
    readfile(10, 20, 'readtest.log')
    # 读取最后10行
    readline(10, 'readtest.log')
    # 读取最后20行
    readfileline(20, 'readtest.log')

 6、常用HTTP认证方式(示例代码为H3C SecPath F1000-AI-25防火墙API)

import requests

USER = 'admin'
PASSWD = 'xxxxxxx'

##########关闭requests https posts请求告警##########
requests.packages.urllib3.disable_warnings()

########## HTTP Basic Auth ###############
from requests.auth import HTTPBasicAuth

url_auth = 'https://x.x.x.x/api/v1/tokens'
http_Basic_token = requests.post(url_auth, auth=HTTPBasicAuth(USER, PASSWD), verify=False)
Basic_token_id = http_Basic_token.json().get('token-id')
headers = {"X-Auth-Token": Basic_token_id, "Content-type": "application/json", "Accept": "application/json"}
url_acl = 'https://x.x.x.x/api/v1/SecurityPolicies/GetRules'
Basic_acl = requests.get(url_acl, headers=headers, verify=False)
print(Basic_acl.json())

########## HTTP摘要式身份认证 ###############
from requests.auth import HTTPDigestAuth

url_auth = 'https://x.x.x.x/api/v1/tokens'
http_Digest_token = requests.post(url_auth, auth=HTTPDigestAuth(USER, PASSWD), verify=False)
http_Digest_token_token_id = http_Basic_token.json().get('token-id')
headers = {"X-Auth-Token": http_Digest_token_token_id, "Content-type": "application/json", "Accept": "application/json"}
url_acl = 'https://x.x.x.x/api/v1/SecurityPolicies/GetRules'
Digest_acl = requests.get(url_acl, headers=headers, verify=False)
print(Digest_acl.json())

########## 自定义 HTTP身份认证 ##########
# 当一个身份认证程序附加到一个请求时,在设置 request 期间就会调用该模块
# 因此,__call__()方法必须执行使身份认证有效所需的操作。
# 某些形式的身份认证还将添加钩子以提供进一步的功能。
class MyAuth(requests.auth.AuthBase):
    def __call__(self, r):
        # Implement my authentication
        return r
url_auth = 'https://x.x.x.x/api/v1/tokens'
requests.get(url_auth, auth=MyAuth(), verify=False)

 7、pandas转换python字典为dataframe

import pandas as pd

pydict = {'Dosage': '1.1.1.1,2.2.2.2,4.4.4.4,5.5.5.5', 'HalfLife': '6.6.6.6,7.7.7.7,8.8.8.8', 'Cmax': '20.20.20.20'}
df = pd.DataFrame(pd.Series(pydict), columns=['IP'])
df = df.reset_index().rename(columns={'index': '组名'})
df.index = [i for i in range(1, len(df.index) + 1)]
print(df)

参考链接:
       https://blog.csdn.net/weixin_47661174/article/details/124697842      # Pandas DataFrame的合并

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值