Update a stored file in any variable in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Update a stored file in any variable in python

Hey guys, Any one help me that how I can update a stored value in any variable into an already existing file. In short I want to update my existing file value into new given value which is thrown by user by keyboard. Help me

8th Sep 2018, 5:28 PM
Ashish Verma
Ashish Verma - avatar
3 Answers
0
You can load the file in a variable as a string, search and replace the needed value using RegEx, then save the string to the file again. If the file is huge, you can load and save a specific section - though this can get difficult if the string changes size.
8th Sep 2018, 7:41 PM
Emerson Prado
Emerson Prado - avatar
0
Can this work at all time... Bcz my value is changed every time when I want..
9th Sep 2018, 1:06 AM
Ashish Verma
Ashish Verma - avatar
0
Ashish Verma It can fail if you variable assume values which confuse the search and replace algorithm (or the Python command syntax itself). It's a matter of thinking about all possible formats your variable can take.
9th Sep 2018, 2:21 AM
Emerson Prado
Emerson Prado - avatar