当前位置:主页 > 软件编程 > Python代码 >

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

时间:2021-06-17 09:16:25 | 栏目: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()

您可能感兴趣的文章:

相关文章