A directory contains 10000 txt files. How can I achieve this if I want to randomly read 100 of them? A directory contains 10000 txt files. How can I achieve this if I want to randomly read 100 of them?
Reply content:
A directory contains 10000 txt files. How can I achieve this if I want to randomly read 100 of them?
Traverse all the files and generate an array of file names. array_rand is randomly extracted from the files. Then you can open the files one by one.
Inefficient. You can try to save the file name list
- Files without indexes can only be traversed to obtain the index at random.
GlobThe official function tests the next 10000 text files, which is about time-consuming.0.25 sIt's a little long, but it's okay. After the index0-10000You just need to randomly retrieve 100 numbers. In addition, you can save the indexes for the next time. You can re-generate the indexes each time you add files.
- If you can rename a file
,.Rename and random fetch1-10000If the number is 100, it is okay.