Python Forum

Full Version: Delete line of file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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
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.