栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python文件和文件路径

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python文件和文件路径

1、全文件名获得路径和文件名

    file_to_Check = FileDirectory[0]
    print(file_to_Check)
    # 输出结果  E:/Python/Class3_RC_CRC_Check/1019_XT/CAN2.asc

    print(os.path.realpath(file_to_Check))
    # 输出结果  E:PythonClass3_RC_CRC_Check1019_XTCAN2.asc

    file_dictionary = os.path.dirname(os.path.realpath(file_to_Check))
    print(file_dictionary)
    # 输出结果  E:PythonClass3_RC_CRC_Check1019_XT

    name_temp = file_to_Check.split('/')
    name = name_temp[-1]
    print(name)
    # 输出结果  CAN2.asc

    rawname_temp = name.split('.')
    rawname = rawname_temp[0]
    print(rawname)
    # 输出结果  CAN2

 2、创建log文件并储存在指定位置

    report_name = file_dictionary + "/" + rawname + "_detail_report.txt"
    log = open(report_name, "w", encoding='utf-8')

 3、全扫描文件夹

# 遍历路径下的所有文件
    for file in os.listdir(path):
    
        # 路径+文件名作为读取的文件
        filename=os.path.join(path,file)
    
        # 判断此文件是文档
        if os.path.isfile(filename): 
        
            # 判断此文件是xls文件并排除掉生成的文档
            if (file.endswith('.xls') and ('汇总' not in file)):                      
                
                print('当前处理的excel为',filename)

                # 打开此文件
                myworkbook = xlrd.open_workbook(filename)
                
                # 获取该文件的所有sheet名
                sheet_names = myworkbook.sheet_names()
                
                # 遍历sheet
                for nm in sheet_names:
                    
                    # 匹配有特征名的sheet
                    if re.search(sheet_key_word, nm):
                    
                        # 获取此sheet
                        table = myworkbook.sheet_by_name(nm)
                       

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/339541.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号