python different ways to read file speed different instances

  • 2021-01-25 07:44:28
  • OfStack

1. Reading by line is slow and time-consuming:


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

2. Read all rows quickly:


 with open('inputFile.txt', 'r') as fRead: 
  srcPaths = fRead.readlines() #txt All strings are read in the list The list of srcPaths 
  random.shuffle(srcPaths) # upset list The order 
  for img_path in srcPaths:
  img_path = img_path.rstrip()

Related articles: