I have a folder with 310 txt files and I want to print 248 random file names (without repetition) from these txt files. I have tried out the following code, but it only outputs 1 random file name instead of 248 file names at once.
import os import random path = "C:PythonPython37-32lindenberg_txt" files = os.listdir(path) index = random.randrange(0, len(files)) print(files[index])
Python version used: 3.7
Advertisement
Answer
import os import random path = r"C:PythonPython37-32lindenberg_txt" files = os.listdir(path) random.shuffle(files) print(files[0:248])