Friday 16 November 2018

windows 7 - How do I open a random folder within a specified directory?


I have photos grouped by albums, with a different folder for each album. I would like to open a folder/album randomly to add variety when viewing photo albums. I already know of an image viewer than can shuffle the viewing order of images; I'm looking for a way to randomly open a folder within a directory to complement it.


The OS I am using is Windows 7.



Answer



This Python script will open a random directory, it takes the working directory to randomize as an argument. You can setup a shortcut to call this as well.


#!/usr/bin/env python
#open-random.py
import os
import sys
import random
import subprocess
if __name__ == "__main__":
if len(sys.argv) == 2:
dirname = sys.argv[1]
li = [f for f in os.listdir(dirname) if os.path.isdir(os.path.join(dirname, f))]
random_dir = li[random.randint(0, len(li)) - 2]
random_dir = os.path.join(dirname, random_dir)
print('opening %s' % (random_dir))
subprocess.call(['explorer.exe', random_dir])
else:
print('Usage: python open-random.py base-directory')

Usage: python open-random.py "c:\photos"


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...