Python .txt文件函数读/写位置

Python .txt文件函数读/写位置,python,python-3.x,function,file,Python,Python 3.x,Function,File,我有一个家庭作业,我需要定义一个函数,该函数执行以下操作: 编写一个函数,给定一个以读写模式打开的文本文件对象和一个字符串,将该字符串的文本插入文件中当前读写位置。换句话说,函数在文件中写入字符串,而不覆盖其余部分。退出函数时,新的读/写位置必须正好位于新插入字符串的末尾。 算法简单;该功能需要: 从当前读/写位置开始读取文件内容 在步骤1开始时的相同位置写入给定字符串 写下步骤1中读取的内容。在执行步骤2的位置。结束 在步骤2的相同位置重新定位读/写光标。结束(步骤3.开始) 我对步骤1-

我有一个家庭作业,我需要定义一个函数,该函数执行以下操作:


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

  • 从当前读/写位置开始读取文件内容
  • 在步骤1开始时的相同位置写入给定字符串
  • 写下步骤1中读取的内容。在执行步骤2的位置。结束
  • 在步骤2的相同位置重新定位读/写光标。结束(步骤3.开始)

  • 我对步骤1-4感到非常困惑,不知道我需要做什么,教授也没有帮助

    下面是我编写的代码,但我不认为函数遵循给定的参数

    def readWrite(file, string):
        file.read()
        file.seek(0)
        file.write(string)
        
    
    give_file = input("enter a file name: ")
    give_string = input("enter a string: ")
    try:
        readFile = open(give_file, "a+")
        file_content = readWrite(readFile, give_string)
    except FileNotFoundError:
        print("File does not exist")
        exit(1)
    
    
    
    整个代码应该要求一个简单的
    .txt
    文件,它将接受该文件和一个字符串,并将其添加到原始文件中

    例如: 该文件是Twinkle.txt

    Twinkle, twinkle, little bat!
    How I wonder what you're at!
    Up above the world you fly,
    Like a teatray in the sky.
    
    输出:

    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.
    

    我想我不知道完整的答案,但我知道它涉及到处理文件流。在python文档中查找
    TextIO
    对象
    file.write(content)
    in
    save\u file
    根据我的经验删除文件的现有内容。我相信您希望在底部以追加模式打开文件,
    open
    中的字符串参数是
    'a'


    此链接可能会有所帮助:

    我找到了方法,下面是函数:

    def readWrite(file, string):
        if not file.readable():
            print("This file is not readable")
            file.close()
            return
        if not file.writable():
            print("This file is not writable")
            file.close()
            return
        initialPosition = file.tell()
        print(initialPosition)
        readContent = file.read()
        file.seek(initialPosition)
        file.write(string)
        secondPosition = file.tell()
        print(secondPosition)
        file.write(readContent)
        file.seek(secondPosition)
    
    基本上,这会接收文件并将文件名和行号附加到每一行。(行号代码在下面的主体部分)

    主体:

    give_file = input("enter a file name: ")
    try:
        openFile = open(give_file, "r+")
        position = openFile.tell()
    except FileNotFoundError:
        print(give_file, " does not exist or could not open")
        exit(1)
    
    counter = 0
    for line in openFile:
        counter += 1
    openFile.seek(position)
    readWrite(openFile, give_file + "\n" + "\n")
    
    spacing = 1
    while spacing <= counter:
        readWrite(openFile, str(spacing) + " ")
        openFile.readline()
        spacing += 1
    
    openFile.close()
    
    give_file=input(“输入文件名:”)
    尝试:
    openFile=open(给出文件“r+”)
    position=openFile.tell()
    除FileNotFoundError外:
    打印(给出文件“不存在或无法打开”)
    出口(1)
    计数器=0
    对于openFile中的行:
    计数器+=1
    openFile.seek(位置)
    读写(openFile,给出文件+“\n”+“\n”)
    间距=1
    
    按步骤1至4间隔时?就像整个任务一样?@matiss这只是功能。主体只是调用函数并对行进行编号(仍在计算),为什么要使用3个不同的退出代码退出?我假设这是为了跟踪程序的功能exited@Matiiss我认为两个功能需要合并为一个。。但是我很困惑,所以我不知道该怎么办it@MMSS19也许可以看看我对类似问题的回答。我很确定这就是你要找的。如果有帮助的话,别忘了投票:)这更适合评论,但鉴于你的代表不允许你通过给出有意义的回答来获得答案,我明白你的意思,这个答案太简短了,但链接不是一个对我来说很重要的教程,这个链接只是因为我不打算用python的
    open
    方法的完整解释来增加我的答案的简洁性