Python Forum
Delete all contents of a file from the fifth line?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete all contents of a file from the fifth line?
#1
Hello,

I want to delete all the contents of the file from under the fifth line.

Is this doable?
I don't want to just print the file on the console, I actually want the contents of the file to be deleted from the fifth line and then to be saved.

I tried searching on the internet but was not able to find anything, maybe I just suck search at searching.

Can anyone help?
Thank you.
Reply
#2
just read the file up to 5th row and save it as temp file, after that replace the original.

or as an alternative is using fileinput with inplace=True
import fileinput

with fileinput.input(files=('sample.dat',), inplace=True) as f:
    for _ in range(5):
        print(next(f), end='') # this will write to original file
Quote:Optional in-place filtering: if the keyword argument inplace=True is passed to fileinput.input() or to the FileInput constructor, the file is moved to a backup file and standard output is directed to the input file (if a file of the same name as the backup file already exists, it will be replaced silently). This makes it possible to write a filter that rewrites its input file in place. If the backup parameter is given (typically as backup='.<some extension>'), it specifies the extension for the backup file, and the backup file remains around; by default, the extension is '.bak' and it is deleted when the output file is closed. In-place filtering is disabled when standard input is read.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,394 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Getting last line of each line occurrence in a file tester_V 1 813 Jan-31-2023, 09:29 PM
Last Post: deanhystad
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,308 Sep-27-2022, 01:38 PM
Last Post: buran
  Delete multiple lines from txt file Lky 6 2,207 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  Print to a New Line when Appending File DaveG 0 1,190 Mar-30-2022, 04:14 AM
Last Post: DaveG
  Find and delete above a certain line in text file cubangt 12 3,355 Mar-18-2022, 07:49 PM
Last Post: snippsat
  [Solved] Delete a line in a dataframe using .reset_index() and .drop() ju21878436312 2 2,637 Feb-25-2022, 02:55 PM
Last Post: ju21878436312
  CSV to Text File and write a line in newline atomxkai 4 2,612 Feb-15-2022, 08:06 PM
Last Post: atomxkai
  How to delete portion of file already processed? Mark17 13 2,639 Jan-22-2022, 09:24 AM
Last Post: Pedroski55
  writelines only writes one line to file gr3yali3n 2 2,296 Dec-05-2021, 10:02 PM
Last Post: gr3yali3n

Forum Jump:

User Panel Messages

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