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:\Python\Class3_RC_CRC_Check\1019_XT\CAN2.asc
file_dictionary = os.path.dirname(os.path.realpath(file_to_Check))
print(file_dictionary)
# 输出结果 E:\Python\Class3_RC_CRC_Check\1019_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)