【问题标题】:Need help making file i/o program using Python需要帮助使用 Python 制作文件 i/o 程序
【发布时间】:2014-04-30 23:07:29
【问题描述】:

如下制作一个密码学程序。(添加文件 i/o 到它)

  1. 您应该(至少)有两个功能:加密和解密。
  2. 增强您的主要例程以: 一个。欢迎用户
    湾。询问用户是否要加密或解密
    C。询问用户加密消息要写入/读取的文件
    d。如果加密:
    要求消息加密
    求钥匙
    调用 encrypt 创建密文
    打开文件
    将密码测试写入文件
    关闭文件
    通知用户密文(显示)已存储到文件中(确保提及文件名!)
    e.如果解密:要求密钥打开文件从文件中读取密码测试关闭文件调用解密以解密密文为用户显示消息
    F。询问用户是否愿意执行另一项任务
    G。如果是,请转到 (b)
    H。否则退出

以下是我需要在程序中添加的代码:

#open file to write record(s). It will create the file if new!
f = open("temp.txt", "w") 
f.write("Hello!\n")
f.close

#open file to read this time
f = open("temp.txt", "r")
line = f.readline()
print(line)
f.close

#try binary read...
f = open("temp.txt", "rb")
line = f.readlines()
print(line)
f.close

这是我已经拥有的(除了文件的输入/输出函数之外的所有内容):

# Caesar Cipher

MAX_KEY_SIZE = 26

def getMode():
    while True:
        mode = input("Do you wish to encrypt or decrypt a message?").lower()
        if mode in "encrypt e decrypt d". split():
            return mode
        else:
##            print("Enter either "encrypt" or "e" or "decrypt" or "d".')
def getMessage():
    return input("Enter you message")

def getKey():
    key = 0
    while True
        key = int(input("Enter a key number (1-26)"))
        if (key >=1 and key <= MAX_KEY_SIZE):
            return key

def getTranslatatedMessage(mode, messafe, key):
    if mode[0] == "d":
        key = -key
    translated = ""

    for symbol in message:
        if symbol.isalpha():
            num = ord(symbol)
            num += key

            if symbol.isupport():
                if num > ord("Z"):
                  num -= 26
                elif num < ord("A"):
                  num += 26
            elif symbol.islower():
                if num < ord("z"):
                  num += 26
                elif num < ord("a"):
                  num += 26

            translated += chr(num)
        else:
            translated += symbol
    return translated
mode = getMode()
message = getMessage()
key = getKey()

我的问题是我在哪里将上述代码添加到加密/解密程序中?

【问题讨论】:

  • 那么你的问题是什么?这听起来像是“你能完成我的家庭作业吗?”。您应提供具体问题。
  • 我制作了加密/解密程序,它运行良好,但现在我必须让它向用户询问文件,我知道这样做的代码,我只是不知道放在哪里他们。
  • 您应将此具体问题写到问题的末尾。它可能像“如何要求用户输入指定文件名”或类似的东西(你比我更清楚)。

标签: python encryption file-io cryptography


【解决方案1】:

你没有问你的问题。

您可以在Python documentation找到有关输入/输出的信息

基本上,您可以按如下方式打开、写入和关闭文件:

    f = open("myfile", "w")
    text = "something"
    f.write(text)
    f.close()

要处理二进制数据,可以指定二进制格式“wb”用于写入或“rb”用于读取和转换,例如:

    f = open("binfile", "rb")
    buffer = f.read(2)
    print hex(ord(buffer[0]))

还有其他方法可以在 Python 中打包/解压缩二进制文件,但您必须提出更准确的问题。

【讨论】:

  • 对不起,我的问题是我不知道在程序的哪里添加文件i/o代码。
【解决方案2】:

您似乎想要处理文件而不是用户提供的消息。 所以在 getMessage 你应该返回读取文件的内容

    def getMessage():
        f = open("temp.txt", "r")
        text = f.read()
        f.close()
        return text

然后您将翻译后的消息写入文件。

【讨论】:

  • 所以我替换: def getMessage(): return input("Enter you message") 为 def getMessage(): f = open("temp.txt", "r") text = f. read() f.close() 返回文本
  • 我添加了您说的代码,但是当它问我是否要加密或解密时,我输入了 encrypt 并且它给了我一条错误消息:您希望加密还是解密消息?加密 Traceback(最近一次调用最后一次):文件“/Users/Ally/Downloads/EncryptDecrypt Program”,第 50 行,在 message = getMessage() 文件“/Users/Ally/Downloads/EncryptDecrypt Program”,第 13 行,在 getMessage f = open("temp.txt", "r") IOError: [Errno 2] No such file or directory: 'temp.txt'
  • 看来这是你的需要。但我认为你应该先了解你想做什么,也许在用任何语言实现它之前编写一个算法
  • 错误很明显!!!文件 temp.txt 应该存在并包含您要加密的文本。
  • 我不会为你编写程序。正如我告诉你的,首先尝试了解你需要做什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
  • 1970-01-01
  • 1970-01-01
  • 2020-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多