刚开始能成功读取文件夹中的图片,后面突然报错,采用了很多办法修改:改绝对路径,检查命名规范,修改格式符都不行,
求各位大shen看看有没有什么好办法,或者换一种函数只要能循环读取文件夹中的图片名并转换为列表都行🙏🙏
os.listdir一直显示 目录名称无效
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
关注 - 这篇博客: os.listdir读取文件夹中文件乱序的方法总结中的 2.最后我用这段代码解决了问题 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
代码如下:
import os def is_number(s): try: float(s) return True except ValueError: pass try: import unicodedata unicodedata.numeric(s) return True except (TypeError, ValueError): pass return False def find_continuous_num(astr, c): num = '' try: while not is_number(astr[c]) and c < len(astr): c += 1 while is_number(astr[c]) and c < len(astr): num += astr[c] c += 1 except: pass if num != '': return int(num) def comp2filename(file1, file2): smaller_length = min(len(file1), len(file2)) continuous_num = '' for c in range(0, smaller_length): if not is_number(file1[c]) and not is_number(file2[c]): # print('both not number') if file1[c] < file2[c]: return True if file1[c] > file2[c]: return False if file1[c] == file2[c]: if c == smaller_length - 1: # print('the last bit') if len(file1) < len(file2): return True else: return False else: continue if is_number(file1[c]) and not is_number(file2[c]): return True if not is_number(file1[c]) and is_number(file2[c]): return False if is_number(file1[c]) and is_number(file2[c]): if find_continuous_num(file1, c) < find_continuous_num(file2, c): return True elif find_continuous_num(file1, c) > find_continuous_num(file2, c): return False # if file1 < file2: # return True # else: # return False def sort_insert(lst): for i in range(1, len(lst)): x = lst[i] j = i while j > 0 and lst[j - 1] > x: # while j > 0 and comp2filename(x, lst[j-1]): lst[j] = lst[j - 1] j -= 1 lst[j] = x return lst def sort_insert_filename(lst): for i in range(1, len(lst)): x = lst[i] j = i # while j > 0 and lst[j-1] > x: while j > 0 and comp2filename(x, lst[j - 1]): lst[j] = lst[j - 1] j -= 1 lst[j] = x return lst def file_name_sort(all_file_list): new_list = [] return new_list dir='' filenames=os.listdir(dir) print(sort_insert_filename(filenames))
解决 无用评论 打赏 举报- 这篇博客: os.listdir读取文件夹中文件乱序的方法总结中的 2.最后我用这段代码解决了问题 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
悬赏问题
- ¥15 鼠标右键,撤销删除 复制 移动,要怎样删除
- ¥15 使用MATLAB进行余弦相似度计算加速
- ¥15 服务器安装php5.6版本
- ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
- ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
- ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
- ¥15 arcgis处理土地利用道路 建筑 林地分类
- ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
- ¥100 寻会做云闪付tn转h5支付链接的技术
- ¥15 DockerSwarm跨节点无法访问问题