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?
#1
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   2.py (Size: 481 bytes / Downloads: 477)
Reply
#2
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.
Reply
#3
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
    f.seek(0)         
    # 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.
Reply
#4
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:]
        revised_file.write(f"{';'.join(row)}\n")
This line it's not needed anymore as it was in Python 2💀
# -*- coding: utf-8 -*-
mjrezayani likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write variable in a python file then import it in another python file? tatahuft 4 1,011 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  Read TXT file in Pandas and save to Parquet zinho 2 1,354 Sep-15-2024, 06:14 PM
Last Post: zinho
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,140 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 1,139 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 6,765 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  Filer and sort files by modification time in a directory tester_V 5 2,592 May-02-2024, 05:39 PM
Last Post: tester_V
  Open/save file on Android frohr 0 1,183 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 9,063 Dec-14-2023, 08:03 AM
Last Post: shanoger
  how to save to multiple locations during save cubangt 1 1,347 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  Need to replace a string with a file (HTML file) tester_V 1 1,989 Aug-30-2023, 03:42 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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