-1

I'm trying to open files in a directory that doesn't have just .jpg files in it here's the code i wrote:

import os
import random
import time, shutil
path=(r"C:\Users\KIMEDIAS\Desktop\Noice\PYTHON")
files=os.listdir(path)
d = random.choice(files)
os.startfile(d)
time.sleep(2)
quit()

Can someone help me so i can specify the file type it open's. And, if you can make it close the process and delete the file after some seconds.

Moosa Saadat
  • 1,159
  • 1
  • 8
  • 20
Celverboy
  • 19
  • 3

2 Answers2

1

You can use glob to get a certain type of files

import glob

path=(r"C:\Users\KIMEDIAS\Desktop\Noice\PYTHON\*.jpg")
files=glob.glob(path)
print(files)
Mace
  • 1,355
  • 8
  • 13
0

Please see this question which should supply the answer your looking for: Find all files in a directory with extension .txt in Python

Donavan White
  • 1,126
  • 10
  • 23