Python随机读取文件实现实例,
Python随机读取文件实现实例,
Python随机读取文件
代码如下
import os import random rootdir = "d:\\face\\train" file_names = [] for parent, dirnames, filenames in os.walk(rootdir): #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字 file_names = filenames # for filename in filenames: #输出文件信息 # print("parent is" + parent) # print("filename is:" + filename) # print("the full name of the file is:" + os.path.join(parent, filename)) x = random.randint(0, len(file_names)-1) print(file_names[x])
注意
1.本代码在Python3.5上测试通过
2.rootdir参数意为你要遍历的那个文件夹的目录,请根据自己的实际需要自行修改
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关内容
- 用生成器来改写直接返回列表的函数方法,生成器返回
- python爬虫入门教程--快速理解HTTP协议(一),python爬虫
- Python操作使用MySQL数据库的实例代码,pythonmysql
- python爬虫入门教程--优雅的HTTP库requests(二),pythonr
- Python爬虫之模拟知乎登录的方法教程,python爬虫
- Python 含参构造函数实例详解,python构造函数
- Python 编码Basic Auth使用方法简单实例,pythonauth
- Python正则表达式完全指南,python完全指南
- python爬虫入门教程--利用requests构建知乎API(三),py
- python win32 简单操作方法,pythonwin32
评论关闭