Created
March 4, 2017 21:55
-
-
Save erm3nda/47546d4743cb99c5ce41a39ab6b3c5dd to your computer and use it in GitHub Desktop.
Python basic txt list manager CLI
This file contains hidden or 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
#!/user/local/env python | |
# -*- coding:utf8 -*- | |
import os | |
import platform | |
filename = "registros" | |
if not os.path.isfile(filename): | |
open(filename, "w+").close() | |
def leer(limit=None): | |
content = open(filename, "r").read().splitlines() | |
return content[:limit] | |
def insertar(nombre, apellido, telf, direccion): | |
query = nombre + ";" + apellido + ";" + telf + ";" + direccion | |
content = [] | |
content = open(filename, "r").read().splitlines() | |
if query in content: | |
print "[info] registro ya presente", query.replace(";", " ") | |
else: | |
content.append(query) | |
content = set(content) # eliminar duplicados | |
with open(filename, "w+") as rfile: | |
for i in content: | |
rfile.write(i + "\n") | |
def clear_screen(): | |
_=os.system("clear") if platform.system() == "Linux" else os.system("cls") | |
def menu(): | |
print "1 - insertar | 2 - leer | 3 - modificar" | |
command = raw_input("introduzca orden o num: ") | |
clear_screen() | |
if command == "": | |
while command == "": | |
clear_screen() | |
print "1 - insertar | 2 - leer | 3 - modificar" | |
command = raw_input("introduzca orden o num: ") | |
elif command == "1" or command == "insertar": | |
nombre = raw_input("Nombre: ") | |
apellido = raw_input("Apellido: ") | |
edad = raw_input("Teléfono: ") | |
direccion = raw_input("Dirección: ") | |
if nombre and apellido and edad and direccion: | |
insertar(nombre, apellido, edad, direccion) # ejecutamos la consulta | |
elif command == "2" or command == "leer": | |
registros = leer() | |
print "obtenidos", str(len(registros)), "regitros" | |
print "" | |
for i in registros: | |
print i.replace(";", " ") | |
elif command == "3" or command == "editar": | |
print "opcion no implementada todavía" | |
print "" | |
if __name__ == "__main__": | |
print " ### registros.txt python ###" | |
while True: | |
menu() # punto de entrada y control principal, siempre se vuelve al menu |
This file contains hidden or 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
demo;user;1234;c/ ola | |
john;doe;666666666;c/ ola ke ase | |
john;doe2;666666666;c/ ola ke ase | |
juan;perico;666444333;C/mi casa, telefono |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment