How to open a random folder within a directory

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

Related videos on Youtube

Sunny Shah
Author by

Sunny Shah

Updated on November 23, 2022

Comments

  • Sunny Shah
    Sunny Shah over 1 year

    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 almost 12 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 en.wikipedia.org/wiki/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 pastebin.com and post the link in your question.
    • Bruno Pereira
      Bruno Pereira almost 12 years
      You should report this issue as a bug in launchpad.net, they will guide you and help you sort out if there is another bug already open with it.
    • artfulrobot
      artfulrobot almost 12 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 almost 12 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 almost 12 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 almost 12 years
      Yep, unresponsive to Alt-SysRq.
    • vchyzhevskyi
      vchyzhevskyi about 11 years
      1) get list of all folder within a directory 2) random.choice(<list of folders>) 3) open directory 4) PROFIT!
    • bereal
      bereal about 11 years
      First of all, what do you mean by opening a folder?
    • Sunny Shah
      Sunny Shah about 11 years
      Sorry I meant a batch file
    • MattDMo
      MattDMo about 11 years
      @coirius - really? It should be 4) ... 5) PROFIT! The "..." is the most important step of all!
  • artfulrobot
    artfulrobot over 11 years
    interesting! I do use Chromium sometimes. However, have def. had freezes without it.
  • artfulrobot
    artfulrobot over 11 years
    ah, shucks, back again. filed bug