Python Forum
fileinput package appears to be zeroing files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
fileinput package appears to be zeroing files
#1
trying to replace a string inline using fileinput package, but after being processed the resulting files are zero bytes... here is a snippet of code, if you need it all just ask :)

for i in files:
        try:
            with open(i) as f:
                data = f.read()
        except Exception as e:
            log_it(logfile, time.time(), "warn", str(e))
            print(e)
        if args.match in data:
            for line in fileinput.input(i, inplace=True, backup=".backup"):
                line = line.replace(args.match, args.replace)
        else:
            # file doesnt contain string, restart the loop
            continue

Fix was to add a print(line) after the replace() statement. Fileinput writes stdout to the file...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error handler appears to be turned off. How do I turn it back on? jpotter0 0 564 Nov-26-2022, 11:44 AM
Last Post: jpotter0
  Including data files in a package ChrisOfBristol 4 2,463 Oct-27-2021, 04:14 PM
Last Post: ChrisOfBristol
  PyPI package and files ChrisOfBristol 4 2,053 Oct-23-2021, 09:17 PM
Last Post: snippsat
  How to convert what appears to be a JSON file to CSV NewBeie 4 2,466 Aug-28-2020, 04:45 PM
Last Post: Larz60+
  Print a certain string only the first time it appears in a test file buttercup 5 2,697 Jul-23-2020, 01:30 PM
Last Post: palladium
  Code works in IDLE, appears to work in CMD, but won't produce files in CMD/Windows ChrisPy33 3 3,195 Jun-12-2019, 05:56 AM
Last Post: ChrisPy33
  Because the emoji appears black and white at the exit ? nerd 3 5,517 Jan-28-2019, 11:34 PM
Last Post: nerd
  unusual box appears DavidinKorea 1 2,247 Jan-12-2019, 07:42 PM
Last Post: Larz60+
  check how many times an item appears in list davidm 8 4,935 Dec-29-2018, 07:49 PM
Last Post: davidm
  Jupyter error - 'The kernel appears to have died, it will restart automatically' meganhollie 5 17,916 Jun-12-2018, 10:11 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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