zoukankan      html  css  js  c++  java
  • struct与subprocess 模块

    struct模块

    pack

    用于将Python的值根据格式符,转换为字符串(因为Python中没有字节(Byte)类型,可以把这里的字符串理解为字节流,或字节数组)。其函数原型为:struct.pack(fmt, v1, v2, …),参数fmt是格式字符串,v1, v2, …表示要转换的python值。

    res = struct.pack("i", 45)
    print(res, len(res))
    运行结果:
    b'-x00x00x00' 4
    格式符C语言类型Python类型 
    x pad byte no value  
    c char string of length 1  
    b signed char integer  
    B unsigned char integer  
    ? _Bool bool  
    h short integer  
    H unsigned short integer  
    i int integer
    I unsigned int integer or long  
    l long integer  
    L unsigned long long  
    q long long long  
    Q unsigned long long long  
    f float float  
    d double float  
    s char[] string  
    p char[] string  
    P void * long

    unpack

    unpack做的工作刚好与struct.pack相反,用于将字节流转换成python数据类型。它的函数原型为:struct.unpack(fmt, string),该函数返回一个元组

    obj = struct.unpack("i", res)
    print(obj[0])
    运行结果:
    45

    calcsize

    用于计算格式字符串所对应的结果的长度,如:struct.calcsize(‘ii’),返回8。因为两个int类型所占用的长度是8个字节。

     subprocess

    用于通过python执行shell命令

    Popen

    res = subprocess.Popen("dir",
                           shell=True,
                           stderr=subprocess.PIPE,
                           stdout=subprocess.PIPE
                           )
    print(res.stderr.read().decode("gbk"))
    print(res.stdout.read().decode("gbk"))
  • 相关阅读:
    NSString 处理
    我的第一个IOSDemo
    NSArray创建和使用
    NSDate
    NSDictionary
    flash全屏代码
    getBounds
    运用递归随机出与上一个数不重复的数
    标签跟随鼠标移动
    保存数据到本地
  • 原文地址:https://www.cnblogs.com/jiaqi-666/p/9585723.html
Copyright © 2011-2022 走看看