Skip to content

Instantly share code, notes, and snippets.

@erm3nda
Created March 4, 2017 21:55
Show Gist options
  • Save erm3nda/47546d4743cb99c5ce41a39ab6b3c5dd to your computer and use it in GitHub Desktop.
Save erm3nda/47546d4743cb99c5ce41a39ab6b3c5dd to your computer and use it in GitHub Desktop.
Python basic txt list manager CLI
#!/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
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