仅迭代文件夹中的几个文件 [英] Iterate through only a couple of files in a folder

查看:78
本文介绍了仅迭代文件夹中的几个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我使用以下方法遍历文件夹中的所有文件:



Currently I loop through all the files in the folder by using:

for eachFile in os.listdir(savePath):





如何快速查看前50个文件?我正在对我的程序进行故障排除,并查看文件夹中的所有~2000个文件需要花费太多时间来循环。



如果我可以减少我看到的文件数量,我可以更快地调试。以下感觉很笨拙...有什么建议使这个超高效?谢谢!





Whats a quick way to look at the only the first 50 files? I am troubleshooting my program and looking at all ~2000 files in the folder takes too much time to loop through.

if I can reduce the number of files I look at I can debug much faster. The following feels clunky... any suggestions on making this ultra efficient? Thank you!

g = []
for eachFile in os.listdir(savePath):
    j = eachFile.strip(".tx")
    g.append(j)

For x in range(0,50):
    

    #Created a list for each file
    w = list(reversed(list(open(savePath + '/' + g[x]))))

推荐答案

将它指向一个文件较少的目录?



或者有一个指向'真实'的牺牲目录dir或你的'减少'目录(视情况而定)使用 mklink [ ^ ]?
point it at a directory with fewer files in it?

or have a sacrificial dir that points to the 'real' dir or your 'reduced' dir (as appropriate) using mklink[^]?


这篇关于仅迭代文件夹中的几个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆