Last active
October 7, 2015 09:48
-
-
Save magnunleno/3146074 to your computer and use it in GitHub Desktop.
nmkdir - criador de diretórios numerados
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
#!/usr/bin/env python | |
# encoding: utf-8 | |
# Licença: GPL3 | |
# Autor: Magnun Leno (magnun [dot] leno [at] gmail [dot] com) | |
import sys | |
from os import mkdir | |
def print_ajuda(): | |
print """ | |
Este comando precisa de pelo menos 2 argumentos: | |
nmkdir <prefixo> <numero> | |
Onde: | |
<prefixo> string que serve de prefixo para o nome da pasta; | |
<numero> numero total de pastas a serem criadas. | |
""" | |
exit(1) | |
def main(nome_pasta, numero): | |
caracteres = len(numero) | |
try: | |
numero = int(numero) | |
except ValueError, e: | |
print_ajuda() | |
frmt_str = "%s%."+str(caracteres)+"i" | |
for n in xrange(numero): | |
diretorio = frmt_str%(nome_pasta, n) | |
try: | |
mkdir(diretorio) | |
except OSError, e: | |
print "Alerta: A pasta '%s' já existe no sistema! Ignorando..."%diretorio | |
exit(0) | |
if __name__ == '__main__': | |
if 2 < len(sys.argv) < 4: | |
main(*sys.argv[1:]) | |
else: | |
print_ajuda() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment