Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python截断方法大小参数行为_Python_File - Fatal编程技术网

Python截断方法大小参数行为

Python截断方法大小参数行为,python,file,Python,File,我试图通过替换字符串的内容并写入更新的内容来更新文件中的内容 现在要做的是,我需要删除旧的内容并写回更新的内容,因此为此,我使用truncate方法 根据truncate 文件。截断([大小]) 尺寸可选。如果存在可选的大小参数,则文件为 截断到(最多)那个大小。大小默认为当前值 位置 现在,我有两个案例,我无法理解到底发生了什么? def fileIsWritingInBinary(): with open("hello.txt", "r+") as f: fileContent

我试图通过替换字符串的内容并写入更新的内容来更新文件中的内容

现在要做的是,我需要删除旧的内容并写回更新的内容,因此为此,我使用
truncate
方法

根据
truncate

文件。截断([大小])

尺寸可选。如果存在可选的大小参数,则文件为 截断到(最多)那个大小。大小默认为当前值 位置

现在,我有两个案例,我无法理解到底发生了什么?

def fileIsWritingInBinary():
  with open("hello.txt", "r+") as f:
    fileContent = f.read()
    f.truncate(0) # used different size argument

    # update new content
    fileContent = fileContent.replace("World", "StackOverFlow")

    # write updated content
    f.write(fileContent)
在文件
hello.txt
中写入的内容采用不同的格式,即

  4865 6c6c 6f00 0000 0000 0000 0048 656c
  6c6f 2c20 576f 726c 6421 
但在进行如下所述的更改后,它工作正常,即在
truncate
调用之前添加
f.seek(0)

def fileIsWritingInBinary():
  with open("hello.txt", "r+") as f:
    fileContent = f.read()

    f.seek(0) // line added
    f.truncate()

    # update new content
    fileContent = fileContent.replace("World", "StackOverFlow")

    # write updated content
    f.write(fileContent)

    # hello.txt content
    # Hello, StackOverFlow!
现在的问题是,

  • 为什么
    truncate(0)
    调用导致文件以不同的格式写入
  • 我已经将size参数从
    0
    更改为不同的数字,但仍然得到了相同的结果

  • 如果您查看
    io
    模块中的文档,您会看到发生了什么:

    将流大小调整为给定的字节大小(如果未指定大小,则调整为当前位置)当前流位置未更改。此调整大小可以扩展或减小当前文件大小。对于扩展名,新文件区域的内容取决于平台(在大多数系统上,额外的字节是零填充的)。将返回新的文件大小

    (强调矿山)

    因此,即使截断流,也没有改变流中的位置(这就是
    seek
    所做的)。编写时如何解决这一问题可能取决于操作系统(在我的计算机上,第一个示例起作用!),但您始终可以让Python
    告诉您流中的当前位置:

    with open("hello.txt", "r+") as f:
        fileContent = f.read()
        print(f.tell())
        f.truncate(0)
        print(f.tell())
        f.seek(0)
        print(f.tell())
    
        # update new content
        fileContent = fileContent.replace("World", "StackOverFlow")
    
        # write updated content
        f.write(fileContent)
        print(f.tell())
    

    谢谢你的回答。知道为什么
    write
    依赖于操作系统吗?或者任何文章来了解它的内部工作。好的,是用C写的,所以可能有些C函数在不同的操作系统上表现不同(或者取决于编译器)。如果您需要任何详细的行为,这可能是最好的(但不一定可以理解)来源: