python 不同方式读取文件速度不同的实例

yipeiwu_com6年前Python基础

1、按行读取较慢较耗时:

 srcFiles = open('inputFile.txt', 'r')
 for file_path in srcFiles:
  file_path = file_path.rstrip()

2、快速读取所有行:

 with open('inputFile.txt', 'r') as fRead: 
  srcPaths = fRead.readlines() #txt中所有字符串读入list列表srcPaths 
  random.shuffle(srcPaths) #打乱list顺序
  for img_path in srcPaths:
  img_path = img_path.rstrip()

以上这篇python 不同方式读取文件速度不同的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python中实现结构相似的函数调用方法

python的dict用起来很方便,可以自定义key值,并通过下标访问,示例如下: 复制代码 代码如下: >>> d = {'key1':'value1', ... '...

python实现对csv文件的列的内容读取

以下代码测试在python2.7 mac上运行成功 import csv with open('/Users/wangzhao/Downloads/test.csv', 'U')...

Django-Rest-Framework 权限管理源码浅析(小结)

Django-Rest-Framework 权限管理源码浅析(小结)

在django的views中不论是用类方式还是用装饰器方式来使用rest框架,django_rest_frame实现权限管理都需要两个东西的配合: authentication_clas...

Python中的引用和拷贝浅析

If an object's value can be modified, the object is said to be mutable. If the value cannot b...

python gdal安装与简单使用

python gdal安装与简单使用

gdal安装 方式一:在网址 https://www.lfd.uci.edu/~gohlke/pythonlibs/ 下载对应python版本的whl文件,在命令行中pip instal...