#python环境获取文件判断里面字符串类型,符合条件则去掉后缀
import os
# 获取指定文件夹下的所有文件
def getFiles(dir):
file_list = []
for root, dirs, files in os.walk(dir):
for file in files:
file_list.append(os.path.join(root, file))
return file_list
# 从文件中获取字符串,并判断字符串类型
def getString(file):
with open(file, 'r', encoding='utf-8') as f:
for line in f:
string = line.strip()
if string.isalpha():
print('字母类型:' + string)
elif string.isdigit():
print('数字类型:' + string)
else:
print('其他类型:' + string)
# 去掉文件名的后缀
def removeSuffix(file):
file_name = os.path.splitext(file)[0]
return file_name
# 主函数
if __name__ == '__main__':
dir = './'
file_list = getFiles(dir)
for file in file_list:
getString(file)
file_name = removeSuffix(file)
print('文件名:' + file_name)