Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete line of file
#1
Hi,

I would like to create a function that when triggered deletes line from a txt file

ex:
a = input ("which movie would you like to delete?")
a = Donnie Darko
print ("+ a, successfully deleted!")
Help, please
Reply
#2
For starters:
1. The resulting input variable will be a string -> line 2 -> 'Donnie Darko' is the value of a, so line 2 is superfluous
2. print ("+ a, successfully deleted!") -> is +a somehow the variable a , you want to print ?
3. The part of the code where you read the txt file is missing, pls add it.

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
You can read files and write files and append to files. You can replace in files, but it is so limited and error prone that it should not be attempted. You cannot remove something from a file.

Now that you know you cannot delete something from a file, how are you going to solve your problem? As a hint the only file operations you need are open, close, read and write.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  finding p's in words of a multi-line text file johneven 5 4,369 Jun-24-2019, 03:41 PM
Last Post: ThomasL
  reading text file and writing to an output file precedded by line numbers kannan 7 10,413 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Command line inputs not printing to Log File ijosefson 1 3,363 Oct-19-2017, 06:41 AM
Last Post: buran

Forum Jump:

User Panel Messages

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