Python 创建新文件的函数

Python 创建新文件的函数,python,python-3.x,Python,Python 3.x,我想创建一个函数user_dialogue(),它要求输入两个文件的名称。此函数需要处理IOError等错误。然后,这两个文件应该通过我创建的另一个函数运行,即加密函数 该程序的工作原理如下: 新加密文件的名称:out_file.txt 要加密的文件名:blah.txt 这导致了一个错误!请再试一次 要加密的文件名:my file.csv 加密完成 这是我目前的代码: def user_dialogue(): file1 = open(input("New name of file: "

我想创建一个函数user_dialogue(),它要求输入两个文件的名称。此函数需要处理IOError等错误。然后,这两个文件应该通过我创建的另一个函数运行,即加密函数

该程序的工作原理如下:

新加密文件的名称:out_file.txt

要加密的文件名:blah.txt

这导致了一个错误!请再试一次

要加密的文件名:my file.csv

加密完成

这是我目前的代码:

def user_dialogue():
    file1 = open(input("New name of file: "), 'w')

    done = False

    while not done:
        try:
            file2 = open(input("Name of file that you want to encrypt: "), 'r')
        except IOError as error:
        print("File doesn't exist! The error is of the type: ", error)
        else:

        file2.close()

        done = True

    encrypt_file(file2,file1)
用户对话()

这是我的函数encrypt_文件:

def encrypt_file(in_file, out_file):
    fr = open(in_file, 'r')
    fileread = fr.read()
    encryptedfile = text_encryption_function.encrypt(fileread)
    fr.close()

    fw = open(out_file, 'a+')
    fw.write(encryptedfile)
    fw.close()

    return in_file, out_file

由于某种原因,代码不起作用!有什么帮助吗?

使用上下文管理器

def user_dialogue():
    try:
        with open(input("Name of file that you want to encrypt: "), 'r') as file2:
            try:
                with open(input("New name of file(encrypted): "), 'w') as file1:
                    encrypt_file(file2, file1)
            except IOError as e3:
                print('No access')

    except FileNotFoundError as e1:
        print('No such file')
    except IOError as e2:
        print('No access')



def encrypt_file(in_file, out_file):
    fileread = in_file.read()
    encryptedfile = text_encryption_function.encrypt(fileread)

    out_file.write(encryptedfile)
使用try/except:

def user_dialogue():
    try:
        file2 = open(input("Name of file that you want to encrypt: "), 'r')
        try:
            file1 = open(input("New name of file(encrypted): "), 'w')
            encrypt_file(file2, file1)
        except IOError as e3:
            print('No access')
        else:
            file1.close()

    except FileNotFoundError as e1:
        print('No such file')
    except IOError as e2:
        print('No access')
    else:
        file2.close()



def encrypt_file(in_file, out_file):
    fileread = in_file.read()
    encryptedfile = text_encryption_function.encrypt(fileread)

    out_file.write(encryptedfile)

将上下文管理器
一起使用:

def user_dialogue():
    try:
        with open(input("Name of file that you want to encrypt: "), 'r') as file2:
            try:
                with open(input("New name of file(encrypted): "), 'w') as file1:
                    encrypt_file(file2, file1)
            except IOError as e3:
                print('No access')

    except FileNotFoundError as e1:
        print('No such file')
    except IOError as e2:
        print('No access')



def encrypt_file(in_file, out_file):
    fileread = in_file.read()
    encryptedfile = text_encryption_function.encrypt(fileread)

    out_file.write(encryptedfile)
使用try/except:

def user_dialogue():
    try:
        file2 = open(input("Name of file that you want to encrypt: "), 'r')
        try:
            file1 = open(input("New name of file(encrypted): "), 'w')
            encrypt_file(file2, file1)
        except IOError as e3:
            print('No access')
        else:
            file1.close()

    except FileNotFoundError as e1:
        print('No such file')
    except IOError as e2:
        print('No access')
    else:
        file2.close()



def encrypt_file(in_file, out_file):
    fileread = in_file.read()
    encryptedfile = text_encryption_function.encrypt(fileread)

    out_file.write(encryptedfile)

encrypt_file()
需要两个字符串,但您正在传递文件(仅关闭其中一个)。传递名称,或将
encrypt_file()
更改为接受文件。请在需要时添加缩进。在python3中,您应该使用上下文管理器
将open(输入(“新文件名:”),“w”)作为file1:
谢谢您的回答!如何更改(encrypt_file),使其可以接受文件作为输入?
encrypt_file()
需要两个字符串,但您正在传递文件(仅关闭了其中一个)。传递名称,或将
encrypt_file()
更改为接受文件。请在需要时添加缩进。在python3中,您应该使用上下文管理器
将open(输入(“新文件名:”),“w”)作为file1:
谢谢您的回答!如何更改(encrypt_file)以便它可以接受文件作为输入?我应该在代码中的什么位置添加try/except?应该是在“打开…”之后吗?
open
会引发
IOError
所以用…:
抱歉,我还是不明白(我对编程非常陌生。感谢您的帮助,但如果有人能解释更多,我们将不胜感激!我应该在代码中的什么位置添加try/except?应该在“with open…”之后吗?
open
会引发
IOError
所以用…:
抱歉,我还是不明白。:)(我对编程非常陌生。感谢您的帮助,但如果有人能解释更多,我们将不胜感激!