Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trying to edit a text file
#8
(Dec-14-2021, 12:36 AM)greatfella Wrote: I tried many iterations, wish I could remember all of them to post. I just tried f.close() at the end and it still didn't work.
It will not work as you try to do inplace of original file.
Look Gribouillis code it work as he use fileinput with inplace=True.
(Dec-14-2021, 12:36 AM)greatfella Wrote: i am trying to replace the entire line as in replace "whiffenhard" with "woo"
Output of Grib code in fn.txt:
Output:
yipee wahooo woo yerp
To get same output with menator01 code change line 9.
new_line = re.sub('whiffenhard', 'woo', line)
He dos a extra step make new list then overwrite original file,then avoid the inplace problem.

A third party module in-place
from in_place import InPlace.

with InPlace('fn.txt') as fp:
    for line in fp:
        line = line.replace('whiffenhard', 'woo')
        fp.write(line)
Output:
yipee wahooo woo yerp
Gribouillis likes this post
Reply


Messages In This Thread
trying to edit a text file - by greatfella - Dec-13-2021, 12:02 AM
RE: trying to edit a text file - by menator01 - Dec-13-2021, 01:01 AM
RE: trying to edit a text file - by greatfella - Dec-13-2021, 03:38 AM
RE: trying to edit a text file - by Gribouillis - Dec-13-2021, 06:22 AM
RE: trying to edit a text file - by greatfella - Dec-13-2021, 05:09 PM
RE: trying to edit a text file - by ibreeden - Dec-13-2021, 06:14 PM
RE: trying to edit a text file - by greatfella - Dec-14-2021, 12:36 AM
RE: trying to edit a text file - by snippsat - Dec-14-2021, 02:17 AM
RE: trying to edit a text file - by greatfella - Dec-14-2021, 02:06 PM
RE: trying to edit a text file - by ndc85430 - Dec-14-2021, 05:56 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,287 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,904 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,433 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,592 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 4,706 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  saving data from text file to CSV file in python having delimiter as space K11 1 2,552 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Web Form to Python Script to Text File to zip file to web wfsteadman 1 2,256 Aug-09-2020, 02:12 PM
Last Post: snippsat
  Convert Excel file to Text file marvel_plato 6 20,502 Jul-17-2020, 01:45 PM
Last Post: marvel_plato
  Edit Json file mcmxl22 1 2,052 Apr-02-2020, 05:41 PM
Last Post: bowlofred
  Rename file from value in text file Nuge93 1 2,277 Jan-20-2020, 03:50 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

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