【问题标题】:read/write a file input and insert a string读/写文件输入并插入字符串
【发布时间】:2021-07-13 01:25:47
【问题描述】:

我需要编写一个函数,它给定一个以读写模式打开的文本文件对象和一个字符串,将字符串的文本插入文件中的当前读/写位置。换句话说,该函数将字符串写入文件而不覆盖其余部分。退出函数时,新的读/写位置必须正好在新插入的字符串的末尾。 算法简单;该功能需要:

  1. 从当前读/写位置开始读取文件内容
  2. 在步骤 1 开始的相同位置写入给定字符串
  3. 将第1步读取的内容写入第2步结束的位置
  4. 将读/写光标重新定位在同一位置 step2。结束(和第 3 步开始)

如果参数文件对象不可读或不可写,该函数应该打印一条消息并立即返回而不更改任何内容。 这可以通过使用方法文件对象方法readable()writable() 来实现。

在主脚本中:

1- 提示用户输入文件名

2- 以读写模式打开文件。如果找不到文件,打印一条消息并退出程序

3- 插入文件名作为文件的第一行,后跟一个空行

4- 在原文每一行的开头插入一个行号和一个空格。

我对如何编写函数和主体感到非常困惑。 到目前为止我只有

def openFile(fileToread):
    print(file.read())


givefile = input("enter a file name: ")
try:
    file = open(givefile, "r+")
    readWriteFile = openFile(file)

except FileNotFoundError:
    print("File does not exist")
    exit(1)

print(givefile, "\n")

这不是很多。

我需要这样的输出:

twinkle.txt

1 Twinkle, twinkle, little bat! 
2 How I wonder what you're at! 
3 Up above the world you fly, 
4 Like a teatray in the sky.

使用的文件是一个简单的 .txt 文件,带有一闪一闪的歌曲 我该怎么做?

【问题讨论】:

    标签: python python-3.x function file


    【解决方案1】:

    基本解决方案

    give_file = input("enter a file name: ")
    
    
    def open_file(file):
        return file.read()
    
    
    def save_file(file, content):
        file.write(content)
    
    
    try:
        # Use this to get the data of the file
        with open(give_file, "r") as fd:
            file_content = open_file(fd)
    except FileNotFoundError:
        print("File does not exist")
        exit(1)
        
    # change the data
    new_content = f'{give_file}\n\n{file_content}'
    
    try:
        # save the data
        with open(give_file, "w") as fd:
            save_file(fd, new_content)
    except FileNotFoundError:
        print("File does not exist")
        exit(1)
    

    这应该会给你预期的结果。

    我询问了r+ 以及在这种情况下如何使用它。我得到了这个答案:

    将光标重置为 0 就可以了

    my_fabulous_useless_string = 'POUET'
    with open(path, 'r+') as fd:
      content = fd.read()
      fd.seek(0)
      fd.write(f'{my_fabulous_useless_string}\n{content}')
    

    所以你的代码是:

    give_file = input("enter a file name: ")
    
    
    def open_file(file):
        return file.read()
    
    
    def save_file(file, content):
        file.write(content)
    
    
    try:
        # Use this to get the data of the file
        with open(give_file, "+r") as fd:
            file_content = open_file(fd)
            new_content = f'{give_file}\n\n{file_content}'
            fd.seek(0)
            save_file(fd, new_content)
    except FileNotFoundError:
        print("File does not exist")
        exit(1)
    

    一个建议

    不要使用函数,它隐藏了一个方法使用了一些副作用(移动光标)的事实。 而是直接调用方法,这样更好:

    give_file = input("enter a file name: ")
    
    try:
        # Use this to get the data of the file
        with open(give_file, "+r") as fd:
            file_content = fd.read()
            new_content = f'{give_file}\n\n{file_content}'
            fd.seek(0)
            fd.write(new_content)
    
    except FileNotFoundError:
        print("File does not exist")
        exit(1)
    

    或者,用基本的解决方案和功能

    def open_file(path):
        with open(path, "r") as fd:
            return fd.read()
    
    
    def save_file(path, content):
        with open(path, 'w') as fd:
            fd.write(content)
    
    
    # get file_name
    file_name = input("enter a file name: ")
    try:
        # Use this to get the data of the file
        file_content = open_file(file_name)
    except FileNotFoundError:
        print("File does not exist")
        exit(1)
    # change the data
    new_content = f'{file_name}\n\n{file_content}'
    # save the data
    save_file(file_name, new_content)
    

    【讨论】:

    • @MMSS19 我编辑了我的答案以提供更好的解决方案并提供一些建议。
    猜你喜欢
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    相关资源
    最近更新 更多