import os
def split_file_path(text_file, file_type):
'''
文件路径拆分
:param text_file:
:return:
'''
try:
res_text = None
if file_type == 'dir':
res_text = os.path.dirname(text_file)
elif file_type == 'file_ext':
res_text = os.path.basename(text_file)
elif file_type == 'file':
file_full_name = os.path.basename(text_file)
res_text = os.path.splitext(file_full_name)[0]
elif file_type == 'ext':
file_full_name = os.path.basename(text_file)
res_text = os.path.splitext(file_full_name)[1][1:]
return res_text if res_text else None
except Exception as e:
print(e)
return None
if __name__ == '__main__':
text_file = r'F:\text\gold.txt'
print(split_file_path(text_file, 'dir'))
print(split_file_path(text_file, 'file_ext'))
print(split_file_path(text_file, 'file'))
print(split_file_path(text_file, 'ext'))