利用python找到指定路径下的全部txt文件。
指定路径中可能又包含文件夹,这一层的文件夹里可能也包含txt文件或者其他文件夹,其他文件夹又包含……等这样循环下去。当然,如果需要的不是txt文件,在代码中只要修改一点点,就可以找到自己想要的文件了。
import osroot = "H:\ab123"def findtxt(path, ret): """Finding the *.txt file in specify path""" filelist = os.listdir(path) for filename in filelist: de_path = os.path.join(path, filename) if os.path.isfile(de_path): if de_path.endswith(".txt"): #Specify to find the txt file. ret.append(de_path) else: findtxt(de_path, ret) ret = []findtxt(root, ret)for path in ret: print(path)
登录后复制
以上就是如何找到一个目录下所有.txt文件的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2259467.html