程序关闭Python

时间:2016-08-29 22:49:43

标签: python unexpectendoffile

当我从IDLE运行此代码时,它运行正常。当我从命令行运行py文件时,我插入文件名并按输入,程序关闭。为什么会这样?我是否需要在代码中编写某种暂停函数,还是其他的东西?

def encrypt(file,key):
    count=0
    encrypt=[]
    tmp=[]        
    openfile=open(file,"rb")        
    msg=openfile.read()        
    openfile.close()

    for i in msg:
        if count>=len(key):
              count=0     
        encrypt.append(i^ord(key[count]))
        count+=1

    for i in range(len(encrypt)):
        tmp.append(encrypt[len(encrypt)-1-i])

    count=0
    encrypt[:]=[]

    for i in tmp:
        if count>=len(key):
              count=0

        encrypt.append(i^ord(key[count]))
        count+=1

    encryptmsg=''.join(chr(e) for e in encrypt)
    ext=input('Saving...File Name (with extension)? ')
    file= open(ext, "wb")
    file.write(str.encode(encryptmsg))
    file.close()

    input("Press Enter to Continue...")

ans = input("""
 1- Encrypt/Decrypt
 0-Exit
-:""")

file_to_encrypt = input("File? ")
key = input("Key? ")
if ans=='1':
    encrypt(file_to_encrypt,key)
else:
    quit()

0 个答案:

没有答案