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设计】。

相关文章

浅谈Python3 numpy.ptp()最大值与最小值的差

numpy.ptp() 是计算最大值与最小值差的函数,用法如下: import numpy as np a = np.array([np.random.randint(0, 20,...

Python cv2 图像自适应灰度直方图均衡化处理方法

Python cv2 图像自适应灰度直方图均衡化处理方法

__author__ = 'Administrator' import numpy as np import cv2 mri_img = np.load('mri_img.npy...

Python 矩阵转置的几种方法小结

我就废话不多说了,直接上代码吧! #Python的matrix转置 matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] def printma...

Python编程中的文件读写及相关的文件对象方法讲解

python文件读写 python 进行文件读写的内建函数是open或file file_hander(文件句柄或者叫做对象)= open(filename,mode) mode: 模式...

django 微信网页授权认证api的步骤详解

微信网页授权认证 根据微信官方文档,网页授权需要四个步骤, - 用户同意授权-获取code - 通过code 获取网页授权access_token - 通过code 获取网页授权...