2

I want to open a random file from a given directory. I tried this:

import os, random
random.choice(os.listdir("C:\\"))

but it's not working. I tried this on the other hand:

import os, random

random.choice([x for x in os.listdir("C:\\") if os.path.isfile(os.path.join("C:\\", x))])

It worked but it's only listing the files in the directory but not starting them. How do I start these files?

CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • 2
    What do you mean by "starting" the files? – Carlos Mermingas Jan 09 '19 at 12:15
  • 2
    Can you be more specific about what "start" means? e.g. open a text file in notepad? Run a batch file? Open the file in python and then do something with the contents? – doctorlove Jan 09 '19 at 12:15
  • 1
    Possible duplicate of [Best way to choose a random file from a directory](https://stackoverflow.com/questions/701402/best-way-to-choose-a-random-file-from-a-directory) – Raman Mishra Jan 09 '19 at 12:22
  • Traceback (most recent call last): File "", line 2, in File "C:\Users\F.R.E.D.R.I.C.K\AppData\Local\Programs\Python\Python36\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 431: character maps to >>> I got this: ^ – ThatPythonDude Jan 09 '19 at 12:38

1 Answers1

2

Your code will get the filename of random file, but you should open the file to view its contents. After the discussion in comments section, the file format is mp3 and can be played using the webbrowser module.

import os, random
import webbrowser
basedir = "C:\\"

file = random.choice([x for x in os.listdir(basedir) if os.path.isfile(os.path.join(basedir, x))])

print("Playing file {}...".format(file))
webbrowser.open(os.path.join(basedir, file))
amanb
  • 5,276
  • 3
  • 19
  • 38
  • I got this:Traceback (most recent call last): File "", line 2, in File "C:\Users\F.R.E.D.R.I.C.K\AppData\Local\Programs\Python\Python36\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 431: character maps to >>> – ThatPythonDude Jan 09 '19 at 12:40
  • This error will appear depending upon the type of data contained in the file. Since, its a random selection, you will be able to open files(and view data contents) if they do not contain special characters which cannot be decoded. If the file contains characters that cannot be decoded, then you will have to take a look at the data in the file. It is not possible to debug unless we know the data itself. – amanb Jan 09 '19 at 12:43
  • they're just mp3 files. – ThatPythonDude Jan 09 '19 at 12:43
  • I've had this code which i found online, it was simple but i lost it. it just opened random files from a specified folder – ThatPythonDude Jan 09 '19 at 12:44
  • Are you trying to play the mp3 file? You may want to explore options in this post: https://stackoverflow.com/questions/20021457/playing-mp3-song-on-python – amanb Jan 09 '19 at 12:48
  • i've seen this post. this one worked " import webbrowser webbrowser.open("C:\Users\Public\Music\Sample Music\Kalimba.mp3")" buti i can't seem to a random file. how i can i add the random option to this. – ThatPythonDude Jan 09 '19 at 12:52
  • so? what do i do? – ThatPythonDude Jan 09 '19 at 13:07
  • I meant I've edited my answer. You should try it out and see if you are able to play the random file. – amanb Jan 09 '19 at 13:09
  • Glad I could help! – amanb Jan 09 '19 at 13:12