在python中可以使用os模块的path方法来判断文件的扩展名,从而判断文件是.doc还是.docx格式。以下是一个示例代码:

import os

def check_file_format(file_path):
    extension = os.path.splitext(file_path)[1].lower()
    if extension == '.docx':
        return 'docx'
    elif extension == '.doc':
        return 'doc'
    else:
        return 'Unknown format'

file_path = 'example.docx'
file_format = check_file_format(file_path)
print(file_format)

在上述示例中,check_file_format函数接受文件路径作为输入,使用os.path.splitext方法获取文件的扩展名,并将其转换为小写字母。然后,使用条件语句判断文件的格式,并返回相应的字符串。在示例中,文件路径为example.docx,因此输出结果为docx

python-docx 判断doc还是docx格式

原文地址: https://www.cveoy.top/t/topic/iNDD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录