-3

How can I randomly read 50% of the number of files:

files = glob.glob("*.txt")

##suppose files are objects

for fi in files:

    #I want to implement through the for loop

    data = np.genfromtxt(fi, ...)

Edit: I want to implement through the for loop, in my other problem, the files are object .

Roman
  • 3,007
  • 8
  • 26
  • 54

1 Answers1

1

Shuffle the files, then cut the list in half. Here, rounded down:

import random

files = glob.glob("*.txt")
random.shuffle(files)
files = files[:len(files) // 2]

for fi in files:

   data = np.genfromtxt(fi, ...)
Thomas
  • 174,939
  • 50
  • 355
  • 478