• os模块


    OS模块:

    功能: 与操作系统交互---控制文件 / 文件夹

    文件操作

    #判断文件是否存在
    os.path.isfile(r'')
    删除文件
    os.remove(r'')
    重命名文件
    os.rename(r'old_file',r'new_file')
    #执行终端代码
    os.system('命令')
    
    os.walk(‘’)
    #返回path的大小
    os.path.getsize(path)
    

    文件夹操作

    #判断是否为文件夹
    os.path.isdir()
    #判断路径是否存在文件(文件夹)
    os.path.exists
    #创建文件夹
    if not os.path.exists(r''):
        os.mkdir(r'')
    删除文件夹
    os.rmdir(r'')
    列出所有的文件
    os.listdir(r'')
    
    当前文件的所在文件夹
    os.getcwd()
    当前文件所在的具体路径
    os.path.abspath(__file__)
    
    文件的文件夹
    os.path.dirname(os.path/dirname(os.path.abs(__file__)))
    拼接文件路径
    os.path.join(os.path.dirname(os.path.abspath(__file__)),'img','test.ipj')
    
    
    os.getcwd()	获取当前工作目录,即当前python脚本工作的目录路径
    
    os.chdir("dirname")	改变当前脚本工作目录;相当于shell下cd
    
    os.curdir	返回当前目录: ('.')
        
    os.pardir	获取当前目录的父目录字符串名:('..')
    
    os.makedirs('dirname1/dirname2')	可生成多层递归目录
    
    os.removedirs('dirname1')	若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
    
    os.mkdir('dirname')	生成单级目录;相当于shell中mkdir dirname
    
    os.rmdir('dirname')	删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
    
    os.listdir('dirname')	列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
    os.remove()	删除一个文件
    
    os.rename("oldname","newname")	重命名文件/目录
    
    os.stat('path/filename')	获取文件/目录信息
    
    os.sep	输出操作系统特定的路径分隔符,win下为"",Linux下为"/"
    
    os.linesep	输出当前平台使用的行终止符,win下为"	
    ",Linux下为"
    "
    
    os.pathsep	输出用于分割文件路径的字符串 win下为;,Linux下为:
    os.name	输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
    
    os.system("bash command")	运行shell命令,直接显示
    
    os.environ	获取系统环境变量
    
    os.path.abspath(path)	返回path规范化的绝对路径
    
    os.path.split(path)	将path分割成目录和文件名二元组返回
    
    os.path.dirname(path)	返回path的目录。其实就是os.path.split(path)的第一个元素
    os.
    path.basename(path)	返回path最后的文件名。如何path以/或结尾,那么就会返回空值。即os.path.split(path)的第二个元素
    
    os.path.exists(path)	如果path存在,返回True;如果path不存在,返回False
    
    os.path.isabs(path)	如果path是绝对路径,返回True
    
    os.path.isfile(path)	如果path是一个存在的文件,返回True。否则返回False
    
    os.path.isdir(path)	如果path是一个存在的目录,则返回True。否则返回False
    
    os.path.join(path1[, path2[, ...]])	将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
    
    os.path.getatime(path)	返回path所指向的文件或者目录的最后存取时间
    
    os.path.getmtime(path)	返回path所指向的文件或者目录的最后修改时间
    
    os.path.getsize(path) 返回path的大小
    
    import os
    ## 代码统计
    def count_code(file_path):
        """通过文件路径计算文件代码量"""
        count = 0
        # tag = False
        # tag2 = False
        with open(file_path, 'r', encoding='utf8') as fr:
            for i in fr:
                # if ('= """' or "= '''") in i:
                #     tag2 = True
                # if tag and (i.startswith('"""') or i.startswith("'''")) and not tag2:
                #     tag = False
                # if tag and not (i.startswith('"""') or i.startswith("'''")) and not tag2:
                #     continue
                if i.startswith('#'):
                    continue
                if i.startswith('
    '):
                    continue
                # if i.startswith('"""') or i.startswith("'''"):
                #     tag = True
                #     continue
                count += 1
        # 计算代码量
        return count
    
    
    def count_all_file_code(top):
        if os.path.isfile(top):
            count = count_code(top)
            return count
    
        # 针对文件夹做处理
        res = os.walk(top)  # 只针对文件夹
        count_sum = 0
        for dir, _, files in res:
            # print(i) # 所有文件夹名
            # print(l) # i文件夹下对应的所有文件名
            for file in files:
                file_path = os.path.join(dir, file)
                if file_path.endswith('py'):  # 判断是否为py文件
                    count = count_code(file_path)
                    count_sum += count
        return count_sum
    
    
    top = r'测试文件路径'
    count_sum = count_all_file_code(top)
    print(f' {top} 代码量统计: {count_sum}')
    
    
    
  • 相关阅读:
    maquee 无缝轮播
    pascal语言中学版整理
    SPFA
    Bellman—Ford算法思想
    序列化
    random 模块 时间模块(time) sys模块 os模块
    1、正则表达式
    1、__del__ 2、item系列 3、__hash__ 4、__eq__
    小总结 面向对象
    1、面向对象内置函数 2、反射 3、内置方法
  • 原文地址:https://www.cnblogs.com/shaozheng/p/11600325.html
Copyright © 2020-2023  润新知