【python】一些常用的小脚本

找到指定后缀的文件列表

找到指定后缀的文件,返回找到的文件路径列表,会递归文件夹。

import os



# 找到指定后缀的文件
def find_type(path:str,fix:str):
    dlist=os.listdir(path)
    file_list=[]
    for i in dlist:
        ps=os.path.join(path, i)
        if os.path.isdir(ps):
            file_list+=find_type(ps,fix)
        else:
            if(ps[-len(fix):]==fix):
                file_list.append(ps)
    return file_list

转换文件编码

示例为把gb2312编码的文件转化为utf8编码。

def conv(file:str):
    s=""
    try:
        with open(file,encoding="gb2312") as f:
            s=f.read()
        os.remove(file)
        with open(file,mode="w+",encoding="utf-8") as f:
            f.write(s)
    except Exception as e:
        print("conv failed",file)

删除文件注释

输入文件名,行注释标签,块注释标签,生成删除注释后的文件保存并覆盖原文件。
例如C语言使用 // 和 /* */ 来注释,调用方式如下:

del_comm("main.c","//",["/*","*/"])
# 删除所有注释
def del_comm(file:str,line_comm:str,blok_comm:list[str]):
    text=""
    try:
        with open(file,encoding="utf-8") as f:
            lines=f.readlines()
    except Exception as e:
        print("decode failed",file)
        return
    for i in range(len(lines)):
        index=lines[i].find(line_comm)
        if(index>=0):
            lstr=lines[i][:index]
        else:
            lstr=lines[i].rstrip()
        if(len(lstr.strip())>0):
            text+=lstr+'\n'
        elif(text[-2:]=='\\\n'):
            text+='\n'
    index_start=0
    text_out=""
    while True:
        index=text.find(blok_comm[0],index_start)
        index_end=text.find(blok_comm[1],index)
        if(index>=0 and index_end>index):
            text_out+= text[index_start:index]
            index_start=index_end+len(blok_comm[1])
        else:
            text_out+=text[index_start:]
            break
    with open(file,mode="w+",encoding="utf-8") as f:
        f.write(text_out)
        

去除过多的空白字符

def simplified(text:str):
  '''
  返回一个新字符串,去除过多的空白字符
  '''
  space=['\t', '\n', '\v', '\f', '\r',  ' ']

  r=""
  start=0
  is_empty=False
  while text[start] in space:
    start+=1
  for i in range(start,len(text)):
    if text[i] in space:
      is_empty=True
    else:
      if(is_empty==True):
        r+=" "
        is_empty=False
      r+=text[i]
  return r

相关推荐

  1. python一些常用小脚

    2024-03-30 21:34:04       20 阅读
  2. Linux 常用一些命令

    2024-03-30 21:34:04       35 阅读
  3. Nmap常用一些参数

    2024-03-30 21:34:04       20 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-30 21:34:04       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-30 21:34:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-30 21:34:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-30 21:34:04       20 阅读

热门阅读

  1. 大三实习小菜蛋之document元素节点

    2024-03-30 21:34:04       17 阅读
  2. Express

    Express

    2024-03-30 21:34:04      18 阅读
  3. C++知识点总结(27):链表

    2024-03-30 21:34:04       16 阅读
  4. math模块篇(八)

    2024-03-30 21:34:04       16 阅读
  5. vue 通过插槽来分配内容

    2024-03-30 21:34:04       15 阅读
  6. SQL中添加数据的方式

    2024-03-30 21:34:04       14 阅读
  7. 基于IvorySQL+Patroni+vip-manager构建高可用集群

    2024-03-30 21:34:04       15 阅读
  8. Flutter页面生命周期

    2024-03-30 21:34:04       16 阅读
  9. 『C++初阶』第一章-命名空间与缺省参数

    2024-03-30 21:34:04       16 阅读
  10. LeetCode题练习与总结:字母异位词分组

    2024-03-30 21:34:04       28 阅读