读取文件夹中的目录

import os
def list_dir(file_dir):
f=open('C:\Users\10107472\Desktopok.txt','w') #打开文件
'''
通过 listdir 得到的是仅当前路径下的文件名,不包括子目录中的文件,如果需要得到所有文件需要递归
'''
# print(' <><><><><><> listdir <><><><><><>')
# print( "current dir : {0}".format(file_dir))
dir_list = os.listdir(file_dir)
for cur_file in dir_list:
# 获取文件的绝对路径
# path = os.path.join(file_dir, cur_file)
# print(cur_file)
# print(type(cur_file))
a=cur_file
b=len(a)
c=b-4
d=a[c-2:c]
print(type(b))
f.write(a[0:3])
f.write(' ')
if d=='-X':
d='擦伤'
if d=='-崩':
d='崩边'
if d=='-划':
d='划伤'
f.write(d)
f.write(' ')
f.close()
f=list_dir('C:\Users\10107472\Desktop\POC测试图片NG(已完成坐标)')
原文地址:https://www.cnblogs.com/tangjunjun/p/11289824.html