elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Curso de javascript por TickTack


  Mostrar Temas
Páginas: [1]
1  Programación / Scripting / Consejos sobre programación en python en: 19 Abril 2014, 05:54 am
Hola gente. como están?
Soy nuevo en el foro asi que paso a presentarme primero. Tengo 24 años, soy de Argentina y hace poco empecé con el tema de la programación y más que nada con Python.

Estoy leyendo mi primer libro sobre este lenguaje:
http://inventwithpython.com/hacking/chapters/

y si bien todavia voy por el capitulo 6 les quería hacer una consulta. Estuve modificando el código ejemplo que da el libro sobre el cifrado caesar para abrir un archivo, leer el contenido, encriptarlo y sobreescribirlo. Seguramente van a encontrar mil errores en las modificaciones que hice, y si bien el código funciona y hace lo que quiero, quisiera saber que maneras hay para abrir archivos y sobreescribirlos sin tener que cerrarlos y volverlos a abrir porque cuando probé con 'w+' o 'w' directamente los dejaba vacios.

Consejos, criticas y todo lo que se les ocurra es bienvenido.

Aquí les dejo el código
Código:
# Caesar Cipher
# http://inventwithpython.com/hacking (BSD Licensed)

# open encrypted/decrypted file
message = open('test.txt','r+')
test = message.read()

# the encryption/decryption key
key = int(input('Key: '))

# tells the program to encrypt or decrypt
mode = input('choose: encrypt / decrypt: ')# set to 'encrypt' or 'decrypt'

# every possible symbol that can be encrypted
LETTERS = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'

# stores the encrypted/decrypted form of the message
translated = ''

# capitalize the string in message
#message = message.upper()

# run the encryption/decryption code on each symbol in the message string
for symbol in test:
    if symbol in LETTERS:
        # get the encrypted (or decrypted) number for this symbol
        num = LETTERS.find(symbol) # get the number of the symbol
        if mode == 'encrypt':
            num = num + key
        elif mode == 'decrypt':
            num = num - key

        # handle the wrap-around if num is larger than the length of
        # LETTERS or less than 0
        if num >= len(LETTERS):
            num = num - len(LETTERS)
        elif num < 0:
            num = num + len(LETTERS)

        # add encrypted/decrypted number's symbol at the end of translated
        translated = translated + LETTERS[num]

    else:
        # just add the symbol without encrypting/decrypting
        translated = translated + symbol

# save encrypted/decrypted file
message.close()
message = open('test.txt','w')
message.write(translated)
message.close()
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines