读取/写入文件输入并插入字符串

2024-10-04 09:27:19 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

如果参数文件对象不可读写,函数应该打印消息并立即返回,而不做任何更改。 这可以通过使用方法file object methodsreadable()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文件 我该怎么做


Tags: 文件对象函数字符串文本txt内容文件名
1条回答
网友
1楼 · 发布于 2024-10-04 09:27:19

基本溶液

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+以及如何在本例中使用它。我得到了这个答案:

reset the cursor to 0 should do the trick

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)

相关问题 更多 >