How to delete a specific line in a file?
First, open the file and get all your lines from the file. Then reopen the file in write mode and write your lines back, except for the line you want to delete:
with open("yourfile.txt", "r") as f:
lines = f.readlines()
with open("yourfile.txt", "w") as f:
for line in lines:
if line.strip("\n") != "nickname_to_delete":
f.write(line)
You need to strip("\n")
the newline character in the comparison because if your file doesn't end with a newline character the very last line
won't either.
Delete or clear a line from a text file
You can open your file in read-write mode and delete the lines that match a condition.
with open(file_path, "r+") as fp:
lines = fp.readlines()
fp.seek(0)
for line in lines:
if "boop" not in line:
fp.write(line)
fp.truncate()
The seek
resets the file pointer.
Reference: using Python for deleting a specific line in a file
How can I delete a line from a text file in C++?
Maybe you can just create a new file and put your data into the new file. Like this:
void Data::delete_line(const string& idNr) {
ifstream list;
ofstream outFile("newList.txt");
string readFile, id;
list.open("list.txt", ios::app);
if (list.is_open()) {
while (getline(list, readFile)) {
int pos = readFile.find(';');
id = readFile.substr(0, pos);
if (idNr != id) {
outFile << readFile;
}
}
}
list.close();
outFile.close();
remove("list.txt");
rename("newList.txt", "list.txt");
}
At the end you just remove your old file and rename the new file with the name of the old file. I hope this will solve your problem.
How to delete a line of text file
This is in winform
. textbox1
is where you enter your word and textbox2
will display the message.
textBox2.Clear();
if (!string.IsNullOrWhiteSpace(textBox1.Text))
{
string LinesToDelete = textBox1.Text;
var Lines = File.ReadAllLines(@"E:\sample.txt");
if (Lines.Contains(textBox1.Text))
{
var newLines = Lines.Where(line => !line.Contains(LinesToDelete));
File.WriteAllLines(@"E:\sample.txt", newLines);
textBox2.Text = "Removed";
}
else
{
textBox2.Text = "Not found";
}
}
If you do not need to show the message, the code below will be sufficient.
var Lines = File.ReadAllLines(@"E:\sample.txt");
var newLines = Lines.Where(line => !line.Contains(LinesToDelete));
File.WriteAllLines(@"E:\sample.txt", newLines);
How to delete a line from a text file in C#?
Read the file, remove the line in memory and put the contents back to the file (overwriting). If the file is large you might want to read it line for line, and creating a temp file, later replacing the original one.
How to delete from a text file, all lines that contain a specific string?
To remove the line and print the output to standard out:
sed '/pattern to match/d' ./infile
To directly modify the file – does not work with BSD sed:
sed -i '/pattern to match/d' ./infile
Same, but for BSD sed (Mac OS X and FreeBSD) – does not work with GNU sed:
sed -i '' '/pattern to match/d' ./infile
To directly modify the file (and create a backup) – works with BSD and GNU sed:
sed -i.bak '/pattern to match/d' ./infile
Delete multiple lines from text file using c#
The logic of your current code only deletes the line equal to line_to_delete
To resolve you would just need to change your if statement.
if (line_number > 20) //i would suggest not hardcoding 20
{
writer.WriteLine(line);
}
Related Topics
Access Image from Different View in a View with Paperclip Gem Ruby on Rails
Making a Pie-Chart of the User Age in Rails
Find a Unique Element in a Compound Array
Transfer Db from One Heroku App to Another Faster
Intermingling Attr_Accessor and an Initialize Method in One Class
How to Escape All Characters with Special Meaning in Regex
How to Insert Erb-Tags with Vim
Rails Console Is Not Outputting SQL Statements to My Development Log
How to Make Private Activities
Emulating Int64 Overflows in Ruby
How to Send Notifications to the User Whose Post Received a Comment
Connecting to Google Analytics API in a Rails App
What's the Differences Between Ruby on Rails and Ruby