randFile.py - Selector aleatorio de archivos
Selecciona aleatoriamente un archivo
Pulse aquí para obtener el archivo
Contenido del Archivo
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# Original file created by Luis Felipe Abad Guzmán
# from the Pulpa linux users group
# http://pulpa.utp.edu.co
# Released under the GPL licence
from sys import *
from random import *
from optparse import OptionParser
from os import *
if __name__ == "__main__":
desc = "Select a random file from <directory> and use it as a command line argument for <action>. <action> may contain any command available to the user. The selected filename and path will be inserted at the end of <action> or in any place where the token $@ is found."
use = "[-d <directory>] [-a <action>]"
parser = OptionParser(usage = use, description = desc)
parser.add_option("-d", "--directory", dest = "dirname", default = "./", metavar = "<directory>", help = "Specify a directory containing some files, one of which will be used. If this option is ommited, then the current directory will be used.")
parser.add_option("-a", "--action", dest = "action", default = "echo \"$@\"", metavar = "<action>", help = "Specify an action string to be used with the path and filename of the selected file. If the action string contains $@ tokens, these will be replaced with \"/path/filename\". Else the \"/path/filename\" string will be appended at the end. Default action is 'echo $@'.")
(Options, args) = parser.parse_args()
selectable = []
#Traverse <directory>. Add filenames to the selectable list
if path.isdir(Options.dirname):
contents = listdir(Options.dirname)
for elem in contents:
if path.isfile(Options.dirname+'/'+elem):
selectable.append(elem)
selected = choice(selectable)
action = Options.action.replace("$@", '"'+path.abspath(Options.dirname+'/'+selected)+'"')
system(action)
