Python-本地
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python-本地相关的知识,希望对你有一定的参考价值。
import os import random class localImages(object): def __init__(self, directory='/home/Foto'): self.directoryScan = directory # Cartella da cui cominciare a prelevare le foto self.directoryRemain = [self.directoryScan] self.filePath = {} def getRandomImages(self): ''' Prelieva dal proprio disco delle immagini in maniera random... ''' for i in range(5): # Scansiona 5 directory if len(self.directoryRemain) > 0: directory = random.choice(self.directoryRemain) self.directoryRemain.remove(directory) files = [] try: files = os.listdir(directory) except: pass # Serve per aumentare il Random delle Foto # FIXME: cercare di aumentare questo random if len(files) > 0: for i in range(len(files)/2): files_remove = random.choice(files) files.remove(files_remove) ########################################## for filename in files: filepath = os.path.join(directory, filename) if os.path.isdir(filepath): self.directoryRemain.append(filepath) elif os.path.isfile(filepath): (name, ext) = os.path.splitext(filepath) if ext.lower() in ('.jpg', '.gif'): self.filePath[filepath] = 0 if len(self.directoryRemain) == 0: self.directoryRemain = [self.directoryScan] def downloadImages(self): ''' Scarica nella cartella localIMGs le foto che vengono trovate nel disco... ''' numberIMGs = len(self.filePath) posIMGs = 1 for imageName in self.filePath: print '[' + str(posIMGs) + '/' + str(numberIMGs) + '] - ' + imageName fread = open(imageName, 'rb') fwrite = open('localIMGs' + os.sep + os.path.split(imageName)[1], 'wb') fwrite.write(fread.read()) fread.close() fwrite.close() posIMGs += 1 if __name__ == '__main__': test = localImages() test.getRandomImages() test.downloadImages() print 'Finito...'
以上是关于Python-本地的主要内容,如果未能解决你的问题,请参考以下文章