win移植linux

win移植linux

python执行shell脚本

  1. os.system("command")

  2. os.popen("command")
    os.popen() 返回的是一个文件对象

  3. subprocess.call()

    print(subprocess.call(["ls","-l"],shell=False))  
    # shell参数为false,则,命令以及参数以列表的形式给出
    
    
    a=subprocess.call(["ls","-l"],shell=False) 
    # shell参数为false,则,命令以及参数以列表的形式给出
    
    p = subprocess.Popen('ps aux',shell=True,stdout=subprocess.PIPE)  
       out,err = p.communicate()  
       for line in out.splitlines():  
           print line  
    

字节码与字符串转换

    #bytes object
    byte = b"byte example"

    # str object
    str = "str example"

    # str to bytes 字符串转字节
    bytes(str, encoding="utf8")

    # bytes to str  字节转字符串
    str(bytes, encoding="utf-8")

    # an alternative method
    # str to bytes  字符串转为字节
    str.encode(str)

    # bytes to str  字节转为字符串
    bytes.decode(bytes)

编码相关

# 编码类型转换, gbk转utf-8
iconv -f gbk -t utf8 target.txt

# 可以通过vim查看与修改文件编码格式
:set fileencoding
:set fileencoding=utf-8
    
# 查看文件编码格式
enca -i -L chinese target.txt

出现:iconv: illegal input sequence at position一般是文件编码类型不正确。

脚本

#!/usr/bin/env python3

import os
import subprocess

suffix = ['cpp', 'h']
for file in os.listdir('./'):
    if any( file.find(fix) != -1 for fix in suffix ):
        cmd = "enca -i -L chinese " + file
        p = subprocess.Popen( cmd, shell=True, stdout=subprocess.PIPE )
        out, err = p.communicate()
        for line in out.splitlines():
            fmt = str(line, encoding='utf-8')
        cmd = "iconv -f " + fmt + " -t utf8 " + file + " > 2.txt"
        subprocess.call( cmd, shell=True )
        cmd = "cat 2.txt > " + file
        subprocess.call( cmd, shell=True )
posted @ 2020-07-29 18:06  friedCoder  阅读(166)  评论(0编辑  收藏  举报