Created
May 9, 2012 15:03
-
-
Save fsouza/2645207 to your computer and use it in GitHub Desktop.
Script de sorteio do dev in cachu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import random | |
import sys | |
limpa_nl = lambda arq: [l.strip('\n') for l in arq.readlines()] | |
def main(): | |
if len(sys.argv) != 2: | |
print "Você deve informar o arquivo com inscritos!" | |
sys.exit(2) | |
nome_arquivo = sys.argv[1] | |
arquivo = None | |
arquivo_sorteados = open("sorteados.txt", "a+") | |
arquivo_sorteados.seek(0) | |
sorteados = limpa_nl(arquivo_sorteados) | |
try: | |
arquivo = open(nome_arquivo) | |
inscritos = limpa_nl(arquivo) | |
sorteado = random.choice(inscritos) | |
while sorteado in sorteados: | |
sorteado = random.choice(inscritos) | |
arquivo_sorteados.write("%s\n" % sorteado) | |
print sorteado | |
except IOError: | |
print "Arquivo %s não existe!" % nome_arquivo | |
finally: | |
if arquivo is not None: | |
arquivo.close() | |
arquivo_sorteados.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment