My primary goal is to write to one file (e.g. file.txt
) in many parallel flows, each flow should start from defined offset of a file.
Example:
script 1 - writes 10 chars from position 0
script 2 - writes 10 chars from position 10
script 3 - writes 10 chars from position 20
I didn't even get to parallelism cause I got stuck on writing to different offsets of a file. I have created a simple script to check my idea:
file = open("sample_file.txt", "w")
file.seek(100)
file.write("new line")
file.close()
Ok, so the file was created, offset was moved to 100
and sentence 'new line'
was added. Success.
But then I wanted to open the same file and add something with offsett 10
:
file = open("sample_file.txt", "w")
file.seek(100)
file.write("new line")
file.close()
file = open("sample_file.txt", "a")
file.seek(10)
file.write("second line")
file.close()
And the sentence 'second line'
is added but at the end of the file.
I'm sure it is possible to add chars somewhere in the middle of a file.
Can anyone help with this simple one?
Or maybe someone has an idea how to do it in parallel?
Pawel