python - 在文件夹中查找下一个文件
问题描述
我在文件夹中获取下一个文件的名称的方法很糟糕,但很有效。我想删除调用 os.listdir 函数的要求,因为它在大文件夹中很慢,但不知道如何。欢迎任何帮助。
注意:fullpathtoimagefile 是旧文件 changeimage 是获取下一个文件的函数的名称
def nextfile(self):
folder, filename = os.path.split(fullpathtoimagefile)
filelist = os.listdir(folder)
nextfile = filelist.index(filename) + 1
nextfilename = filelist[nextfile]
changeimage(os.path.join(folder, nextfilename))
tl;dr 获取文件夹中下一个文件全名的更有效方法
解决方案
简单的代码..
import os
arq = (os.listdir(".")) # "." = directory name
for file in arq:
print(file)
另一个例子:
import os
arq = (os.listdir("."))
for file in arq:
if (os.path.splitext(file)[1] == ".jpg"): #if a JPG file, do something
print(file)
推荐阅读
- visual-studio - 快速查找不记得正则表达式
- arrays - 展开运算符 redorders 数组按字母顺序排列
- python - 在 Python Pandas 中将对象更改为浮点数或数字
- antlr - 从 ANTLR4 语法文件中抛出自定义异常
- sql - 如何从 SQL Server 中的长字符串中提取日期
- javascript - async / await + yield / 下一个执行顺序
- javascript - 无法读取未定义的属性“类别”(错误)
- javascript - 将 @Input 装饰器值传递给派生类中的 super
- json - 如何将 POJO 对象传输到 Ignite 表?
- python - fcluster() 和 fclusterdata() 结果有什么区别吗?