Python Forum
How to save modification of a file in original file not a new one?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to save modification of a file in original file not a new one?
Dear all,

I have a code to modify a file. This code make changes on my file and save it to a new file. But, I want to have changes(modifications) on my original file not a new one. I made attempts to change the code but it didn't work. How can I modify my code to save modification on my original file? My code is attached here.

Thanks alot

Attached Files

.py (Size: 481 bytes / Downloads: 147)
The normal way is to write a temporary file, then move the temporary file to the original place in the file system.

Alternately, you can try the fileinput.FileInput class and its 'inplace' keyword argument to perform such tasks.
One way is to read/modify data from file to some datastructure (readlines?) and then move pointer to the beginning of file and write modified data back (writelines?). In order to do so file must be opened in 'r+' mode, something along those lines:

with open('my_file.txt', 'r+') as f:
    # read and modify data; readlines, iterating or something else         
    # write back modified data
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
mjrezayani Wrote:But, I want to have changes(modifications) on my original file not a new one.
I have used in-place some times,It's easy to use
When using csv module most rewriter a little so it write like writerow(csv module).
Quick test.
# -*- coding: utf-8 -*-
Created on Wed Jun  2 21:02:44 2021

@author: LENOVO

import csv
from in_place import InPlace

with InPlace('csv_data.csv', encoding='utf-8', backup='otherfile.csv') as revised_file:
    reader = csv.reader(revised_file, delimiter=';')
    writer = csv.writer(revised_file, delimiter=';', quoting=0)
    for idx, row in enumerate(reader, start=1):
        if idx >= 3:
            row.insert(1, row[1][0])
            row[2] = row[2][1:]
This line it's not needed anymore as it was in Python 2💀
# -*- coding: utf-8 -*-
mjrezayani likes this post

Possibly Related Threads…
Thread Author Replies Views Last Post
  Create a 2-channel numpy file from two cvs file containing a 9x9 matrix silvialecc 1 128 Yesterday, 07:59 AM
Last Post: Gribouillis
  Convert legacy print file to XLSX file davidm 1 200 Oct-17-2021, 05:08 AM
Last Post: davidm
  Looping through nested elements and updating the original list Alex_James 3 361 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  How to save Matplot chart to temp file? Morkus 2 846 Jun-12-2021, 10:52 AM
Last Post: Morkus
  Read file, reformat and write new file bryanmartin113 1 623 Jun-08-2021, 09:27 PM
Last Post: Larz60+
  How to save some results in .txt file with Python? Melcu54 4 1,486 May-26-2021, 08:15 AM
Last Post: snippsat
  Subprocess.Popen() not working when reading file path from csv file herwin 13 2,182 May-07-2021, 03:26 PM
Last Post: herwin
  How to design a save file format? philipbergwerf 5 1,258 Apr-26-2021, 07:39 PM
Last Post: Gribouillis
  code for CSV file to html file without pandas jony057 1 718 Apr-24-2021, 09:41 PM
Last Post: snippsat
  Copy column from one existing excel file to another file mkujawsk 0 757 Apr-14-2021, 06:33 PM
Last Post: mkujawsk

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020