zoukankan      html  css  js  c++  java
  • 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 )
    
  • 相关阅读:
    sql优化-mysql的慢查询
    LInux服务器防火墙-开放端口
    vim打开文件中文乱码解决方法总结
    查看指定文件夹或文件总的大小,文件夹下各个文件的大小
    grep -v 反选匹配内容(not操作)以及grep -E(or操作)
    查看Liunx服务器的磁盘使用情况df命令,以及查看磁盘分区lsblk命令
    top发现僵尸进程
    查看linux服务器内存使用情况
    GitHub 和 GitLab对比
    git与svn
  • 原文地址:https://www.cnblogs.com/friedCoder/p/13398698.html
Copyright © 2011-2022 走看看