How to open a random folder within a directory

>>> import random
>>> import os
>>> files = os.listdir('/tmp')
>>> dirs = [f for f in files if os.path.isdir(f)]
>>> random.sample(dirs,1)

Related videos on Youtube

Sunny Shah
Author by

Sunny Shah

Updated on November 23, 2022


  • Sunny Shah
    Sunny Shah 9 minutes

    I was wondering how I would use a batch file or Python to open a random folder from a selection of many folders within a directory?

    • Tinellus
      Tinellus over 10 years
      You should be able to elegantly reboot your system by pressing Alt - SysRq (both at the same time) and combine with these, one after the other: r e i s u b (aka the magic SysRq key Could you report back if this works for you or not? You could also check in var/log/syslog and var/log/kern.log (open the log files in gedit) if something out of the ordinary has happened at the time of the freeze/lockup. If there is something, copy the relevant messages to and post the link in your question.
    • Bruno Pereira
      Bruno Pereira over 10 years
      You should report this issue as a bug in, they will guide you and help you sort out if there is another bug already open with it.
    • artfulrobot
      artfulrobot over 10 years
      @Tinellus Will try the magic SysRq thing on my box that has a SysRq button (the laptop doesn't). There is nothing in syslog, guess it doesn't have time to write it. @Bruno i guess you mean ubuntu-bug linux ?
    • jpetersen
      jpetersen over 10 years
      if you have a "Print Screen" button you can use Ctrl + Alt + PrtScrn....just incase you were overlooking that key on your laptop
    • artfulrobot
      artfulrobot over 10 years
      Thanks, not had much success with that in the past, perhaps I'm running out of fingers!. Will report back when I crash it today.
    • artfulrobot
      artfulrobot over 10 years
      Yep, unresponsive to Alt-SysRq.
    • vchyzhevskyi
      vchyzhevskyi over 9 years
      1) get list of all folder within a directory 2) random.choice(<list of folders>) 3) open directory 4) PROFIT!
    • bereal
      bereal over 9 years
      First of all, what do you mean by opening a folder?
    • Sunny Shah
      Sunny Shah over 9 years
      Sorry I meant a batch file
    • MattDMo
      MattDMo over 9 years
      @coirius - really? It should be 4) ... 5) PROFIT! The "..." is the most important step of all!
  • artfulrobot
    artfulrobot about 10 years
    interesting! I do use Chromium sometimes. However, have def. had freezes without it.
  • artfulrobot
    artfulrobot about 10 years
    ah, shucks, back again. filed bug