0

How to write array back to file and replac old one to newOne in python

I have semster projct in which i have to read data from file to array and then performing diffrnt functions like add delet update sort and then i have to writeback the update array to file how can i achieve that i have done all the function like reading from file sorting deleting editing but stuck at writingback the updated array to original with out overwriting data. Any help or suggestions would be appreciated Thanks #happy_Peogramming

22nd Dec 2019, 10:37 AM
Abdul Wahab
Abdul Wahab - avatar
5 Answers
+ 2
With the seek function, you can go precisely to the place where you wand to override the datas, and simply rewrite them. Then only thing is that you must have the same data length. Another solution is to keep all the file in memory. Then you work with the datas in memory and you rewrite the whole file. If you don't want to rewrite the datas but add the new array at the end of the file, open the file in 'a'ppend mode. (open(file, 'a'))
22nd Dec 2019, 11:08 AM
Théophile
Théophile - avatar
+ 2
You can open and read the file like this : with open(file, "r") as doc: datas = list(doc.read().split("\n")) # datas contains each line it the file # works with datas with open(file, "w") as doc: doc.write("\n".join(datas)) # write back the modified datas in the file
22nd Dec 2019, 11:14 AM
Théophile
Théophile - avatar
+ 2
Théophile how to do it in java?
22nd Dec 2019, 11:26 AM
Abdul Wahab
Abdul Wahab - avatar
+ 1
I don't really know java. I think you should read some tutorials about files in Java. There are a lot on the internet.
22nd Dec 2019, 12:38 PM
Théophile
Théophile - avatar
0
How can i rewrite the whole file' ? Théophile
22nd Dec 2019, 11:10 AM
Abdul Wahab
Abdul Wahab - avatar