Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #-------------------------------------------------------------------------------
- # Name: module1
- # Purpose:
- #
- # Author: JULIO
- #
- # Created: 24/08/2014
- # Copyright: (c) JULIO 2014
- # Licence: <your licence>
- #-------------------------------------------------------------------------------
- #!/usr/bin/env python
- import os, shutil
- import random, datetime, time
- ext2conttype = {"jpg": "image/jpeg",
- "jpeg": "image/jpeg",
- "png": "image/png",
- "gif": "image/gif"}
- def content_type(filename):
- return ext2conttype[filename[filename.rfind(".")+1:].lower()]
- def isimage(filename):
- """true if the filename's extension is in the content-type lookup"""
- filename = filename.lower()
- return filename[filename.rfind(".")+1:] in ext2conttype
- def random_file(dir):
- """returns the filename of a randomly chosen image in dir"""
- images = [f for f in os.listdir(dir) if isimage(f)]
- return random.choice(images)
- if __name__ == "__main__":
- elegidofile = ''
- images = []
- ubicacion = '/Users/julio/Documents/mis_moviles/london.jpg' # CHANGE1 : where you put the choosen picture
- dir = '/Users/Shared/fotos' # CHANGE2 : where you have all your pictures to choose
- for (path, dirs, files) in os.walk(dir):
- for name in files:
- if isimage(name):
- #print name
- images.append(path + '/' +name)
- if len(images)>0:
- elegidofile = random.choice(images)
- #print elegidofile
- if elegidofile<>'':
- shutil.copy(elegidofile, ubicacion)
- fcual = open('/Users/julio/Documents/06-zaprogramas/python/personal/random_picture.log','a') # CHANGE3 : where you save your log
- ahora = datetime.datetime.now().strftime("%d-%m-%Y %H:%M")
- fcual.write(ahora + chr(9)+ elegidofile+'\n')
- fcual.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement