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?
#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


Messages In This Thread
RE: How to save modification of a file in original file not a new one? - by snippsat - Jun-04-2021, 01:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 315 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 1,087 Dec-14-2023, 08:03 AM
Last Post: shanoger
  how to save to multiple locations during save cubangt 1 543 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  Need to replace a string with a file (HTML file) tester_V 1 761 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can I change the uuid name of a file to his original file? MaddoxMB 2 921 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  save values permanently in python (perhaps not in a text file)? flash77 8 1,205 Jul-07-2023, 05:44 PM
Last Post: flash77
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,091 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,020 Feb-20-2023, 07:19 PM
Last Post: avd88
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,111 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Save multiple Parts of Bytearray to File ? lastyle 1 939 Dec-10-2022, 08:09 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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