Delete arbitrary file fragment

What is the most efficient way to remove an arbitrary fragment of a file, taking into account the initial and final offsets? I would prefer to use Python, but I can go back to C if I need to.

Say it is a file

..............xxxxxxxx----------------

I want to remove it:

..............[xxxxxxxx]----------------

After the operation, he should become:

..............----------------

Reading all of this into memory and manipulating it in memory is not possible.

+3
source share
3 answers

, , , ( SSD s, ). , - ​​ , . ( , ;-), , , ( , , - , "" RAID- , ; -).

, : , (); N1 ; N2 ; ; ; . ( Windows, -, " " , Python, - , , : , , - - , ).

N1 N2, , , , , . with open('old.dat', 'rb') as oldf: with open('NEWold.dat', 'wb') as newf:, , ( , , ).

" " shutil.copyfileobj ( , , ). "skip" - seek oldf. N1 oldf newf Python, , :

def copyN1(oldf, newf, N1, buflen=1024*1024):
    while N1:
      newf.write(oldf.read(min(N1, buflen)))
      N1 -= buflen
+4

. , , .

, , * nix, Win ( , ).

0

Try mmapin the file. It will not be necessary to immediately read everything in memory.

If you really want to do this manually, select the block size and do a second read and write. But the quest is about to kill you ...

0
source

Source: https://habr.com/ru/post/1760930/


All Articles