Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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中,附加到第n个字节后的文件_Python_File - Fatal编程技术网

在python中,附加到第n个字节后的文件

在python中,附加到第n个字节后的文件,python,file,Python,File,我需要将附加到文件的第n个字节后,而不删除以前的内容 比如说,, 如果我有一个包含“Hello World”的文件 我试图在第(5)位写下我应该得到的“这个” “你好,这个世界” 是否有任何模式可以打开文件 当前我的代码将替换字符 并说“你好,这是” 有什么建议吗?您无法在文件中插入。通常的做法是: 有两个缓冲区,旧文件和要添加内容的新文件 从旧内容复制到新内容,直到插入新内容为止 在新文件中插入新内容 继续从旧缓冲区写入新缓冲区 (可选)将旧文件替换为新文件 在python中,应该是这样的:

我需要附加到文件的第n个字节后,而不删除以前的内容

比如说,, 如果我有一个包含“Hello World”的文件
我试图在第(5)位写下我应该得到的“这个”
“你好,这个世界”

是否有任何模式可以打开文件

当前我的代码将替换字符
并说“你好,这是”


有什么建议吗?

您无法在文件中插入
。通常的做法是:

  • 有两个缓冲区,旧文件和要添加内容的新文件
  • 从旧内容复制到新内容,直到插入新内容为止
  • 在新文件中插入新内容
  • 继续从旧缓冲区写入新缓冲区
  • (可选)将旧文件替换为新文件
  • 在python中,应该是这样的:

    nth_byte = 5
    with open('old_file_path', 'r') as old_buffer, open('new_file_path', 'w') as new_buffer:
        # copy until nth byte
        new_buffer.write(old_buffer.read(nth_byte))
        # insert new content
        new_buffer.write('this')
        # copy the rest of the file
        new_buffer.write(old_buffer.read())
    
    import mmap
    
    with open('hello.txt', 'w') as f:
        # create a test file
        f.write('Hello World')
    
    with open('hello.txt','r+') as f:
        # insert 'this' into that 
        mm=mmap.mmap(f.fileno(),0)
        print mm[:]
        idx=mm.find('World')
        f.write(mm[0:idx]+'this '+mm[idx:])
    
    with open('hello.txt','r') as f:  
        # display the test file  
        print f.read()
        # prints 'Hello this World'
    
    现在您必须在
    new\u buffer
    中拥有
    Hello this world
    。之后,由您决定是用新的覆盖旧的还是您想用它做的任何事情


    希望这有帮助

    在文件中插入
    是不可能的。通常的做法是:

  • 有两个缓冲区,旧文件和要添加内容的新文件
  • 从旧内容复制到新内容,直到插入新内容为止
  • 在新文件中插入新内容
  • 继续从旧缓冲区写入新缓冲区
  • (可选)将旧文件替换为新文件
  • 在python中,应该是这样的:

    nth_byte = 5
    with open('old_file_path', 'r') as old_buffer, open('new_file_path', 'w') as new_buffer:
        # copy until nth byte
        new_buffer.write(old_buffer.read(nth_byte))
        # insert new content
        new_buffer.write('this')
        # copy the rest of the file
        new_buffer.write(old_buffer.read())
    
    import mmap
    
    with open('hello.txt', 'w') as f:
        # create a test file
        f.write('Hello World')
    
    with open('hello.txt','r+') as f:
        # insert 'this' into that 
        mm=mmap.mmap(f.fileno(),0)
        print mm[:]
        idx=mm.find('World')
        f.write(mm[0:idx]+'this '+mm[idx:])
    
    with open('hello.txt','r') as f:  
        # display the test file  
        print f.read()
        # prints 'Hello this World'
    
    现在您必须在
    new\u buffer
    中拥有
    Hello this world
    。之后,由您决定是用新的覆盖旧的还是您想用它做的任何事情


    希望这有帮助

    我认为您要做的是读取文件,将其分成两块,然后重写。比如:

    n = 5
    new_string = 'some injection'
    
    with open('example.txt','rw+') as f:
        content = str(f.readlines()[0])
        total_len = len(content)
        one = content[:n]
        three = content[n+1:total_len]
        f.write(one + new_string + three)
    

    我想你要做的是读取文件,把它分成两块,然后重写它。比如:

    n = 5
    new_string = 'some injection'
    
    with open('example.txt','rw+') as f:
        content = str(f.readlines()[0])
        total_len = len(content)
        one = content[:n]
        three = content[n+1:total_len]
        f.write(one + new_string + three)
    
    您可以使用来执行以下操作:

    nth_byte = 5
    with open('old_file_path', 'r') as old_buffer, open('new_file_path', 'w') as new_buffer:
        # copy until nth byte
        new_buffer.write(old_buffer.read(nth_byte))
        # insert new content
        new_buffer.write('this')
        # copy the rest of the file
        new_buffer.write(old_buffer.read())
    
    import mmap
    
    with open('hello.txt', 'w') as f:
        # create a test file
        f.write('Hello World')
    
    with open('hello.txt','r+') as f:
        # insert 'this' into that 
        mm=mmap.mmap(f.fileno(),0)
        print mm[:]
        idx=mm.find('World')
        f.write(mm[0:idx]+'this '+mm[idx:])
    
    with open('hello.txt','r') as f:  
        # display the test file  
        print f.read()
        # prints 'Hello this World'
    
    mmap
    允许您将字符串视为可变字符串。但它有一些限制,例如切片分配必须与长度相同。可以在mmap对象上使用正则表达式

    底线是,要将字符串插入到文件流中,您需要读取它,将字符串插入到读取的数据中,然后将其写回

    您可以使用来执行以下操作:

    nth_byte = 5
    with open('old_file_path', 'r') as old_buffer, open('new_file_path', 'w') as new_buffer:
        # copy until nth byte
        new_buffer.write(old_buffer.read(nth_byte))
        # insert new content
        new_buffer.write('this')
        # copy the rest of the file
        new_buffer.write(old_buffer.read())
    
    import mmap
    
    with open('hello.txt', 'w') as f:
        # create a test file
        f.write('Hello World')
    
    with open('hello.txt','r+') as f:
        # insert 'this' into that 
        mm=mmap.mmap(f.fileno(),0)
        print mm[:]
        idx=mm.find('World')
        f.write(mm[0:idx]+'this '+mm[idx:])
    
    with open('hello.txt','r') as f:  
        # display the test file  
        print f.read()
        # prints 'Hello this World'
    
    mmap
    允许您将字符串视为可变字符串。但它有一些限制,例如切片分配必须与长度相同。可以在mmap对象上使用正则表达式


    底线是,要将字符串插入到文件流中,您需要读取它,将字符串插入到读取的数据中,然后将其写回

    文件抽象一般不支持“插入”。你要么重写文件的其余部分,要么找到一个更高层次的抽象(这将在封面下为你完成)。我在没有完全理解问题的情况下添加了一个答案(现在已删除)。对于所需内容,有两个标准答案:a)对于大数据,使用数据库,例如sqlite;b)对于小数据,读取文件,执行内存插入,然后重写文件。文件抽象通常不支持“插入”。你要么重写文件的其余部分,要么找到一个更高层次的抽象(这将在封面下为你完成)。我在没有完全理解问题的情况下添加了一个答案(现在已删除)。对于所需内容,有两个标准答案:a)对于大数据,使用数据库,例如sqlite;b)对于小数据,读取文件,执行内存插入,然后重写文件。