How to delete blank lines in a text file on a windows machine
I am trying to delete all blank lines in all YAML files in a folder. I have multiple lines with nothing but CRLF (using Notepad++), and I can't seem to eliminate these blank lines. I researched this before posting, as always, but I can't seem to get this working.
import glob
import re
path = 'C:\Users\ryans\OneDrive\Desktop\output\*.yaml'
for fname in glob.glob(path):
with open(fname, 'r') as f:
sfile = f.read()
for line in sfile.splitlines(True):
line = sfile.rstrip('rn')
f = open(fname,'w')
f.write(line)
f.close()
Here is a view in Notepad++
I want to delete the very first row shown here, as well as all other blank rows. Thanks.
python python-3.x blank-line
add a comment |
I am trying to delete all blank lines in all YAML files in a folder. I have multiple lines with nothing but CRLF (using Notepad++), and I can't seem to eliminate these blank lines. I researched this before posting, as always, but I can't seem to get this working.
import glob
import re
path = 'C:\Users\ryans\OneDrive\Desktop\output\*.yaml'
for fname in glob.glob(path):
with open(fname, 'r') as f:
sfile = f.read()
for line in sfile.splitlines(True):
line = sfile.rstrip('rn')
f = open(fname,'w')
f.write(line)
f.close()
Here is a view in Notepad++
I want to delete the very first row shown here, as well as all other blank rows. Thanks.
python python-3.x blank-line
1
I am afraid that you cannot read and write to the same file at the same time without using seek(). Usually the better way is to create a temporary file, write the change there and then replace the original one (or rename the original to .backup or something). That way if the program fails in the middle you still have a consistent state. Otherwise it should be enough to addif line != '':
after therstrip()
. So you really skip the empty lines.
– petrch
Nov 21 '18 at 15:34
add a comment |
I am trying to delete all blank lines in all YAML files in a folder. I have multiple lines with nothing but CRLF (using Notepad++), and I can't seem to eliminate these blank lines. I researched this before posting, as always, but I can't seem to get this working.
import glob
import re
path = 'C:\Users\ryans\OneDrive\Desktop\output\*.yaml'
for fname in glob.glob(path):
with open(fname, 'r') as f:
sfile = f.read()
for line in sfile.splitlines(True):
line = sfile.rstrip('rn')
f = open(fname,'w')
f.write(line)
f.close()
Here is a view in Notepad++
I want to delete the very first row shown here, as well as all other blank rows. Thanks.
python python-3.x blank-line
I am trying to delete all blank lines in all YAML files in a folder. I have multiple lines with nothing but CRLF (using Notepad++), and I can't seem to eliminate these blank lines. I researched this before posting, as always, but I can't seem to get this working.
import glob
import re
path = 'C:\Users\ryans\OneDrive\Desktop\output\*.yaml'
for fname in glob.glob(path):
with open(fname, 'r') as f:
sfile = f.read()
for line in sfile.splitlines(True):
line = sfile.rstrip('rn')
f = open(fname,'w')
f.write(line)
f.close()
Here is a view in Notepad++
I want to delete the very first row shown here, as well as all other blank rows. Thanks.
python python-3.x blank-line
python python-3.x blank-line
edited Nov 21 '18 at 16:00
ryguy72
asked Nov 21 '18 at 15:26
ryguy72ryguy72
4,4431822
4,4431822
1
I am afraid that you cannot read and write to the same file at the same time without using seek(). Usually the better way is to create a temporary file, write the change there and then replace the original one (or rename the original to .backup or something). That way if the program fails in the middle you still have a consistent state. Otherwise it should be enough to addif line != '':
after therstrip()
. So you really skip the empty lines.
– petrch
Nov 21 '18 at 15:34
add a comment |
1
I am afraid that you cannot read and write to the same file at the same time without using seek(). Usually the better way is to create a temporary file, write the change there and then replace the original one (or rename the original to .backup or something). That way if the program fails in the middle you still have a consistent state. Otherwise it should be enough to addif line != '':
after therstrip()
. So you really skip the empty lines.
– petrch
Nov 21 '18 at 15:34
1
1
I am afraid that you cannot read and write to the same file at the same time without using seek(). Usually the better way is to create a temporary file, write the change there and then replace the original one (or rename the original to .backup or something). That way if the program fails in the middle you still have a consistent state. Otherwise it should be enough to add
if line != '':
after the rstrip()
. So you really skip the empty lines.– petrch
Nov 21 '18 at 15:34
I am afraid that you cannot read and write to the same file at the same time without using seek(). Usually the better way is to create a temporary file, write the change there and then replace the original one (or rename the original to .backup or something). That way if the program fails in the middle you still have a consistent state. Otherwise it should be enough to add
if line != '':
after the rstrip()
. So you really skip the empty lines.– petrch
Nov 21 '18 at 15:34
add a comment |
3 Answers
3
active
oldest
votes
If you use python, you can update the line using:
re.sub(r'[srn]','',line)
Close the reading file handler before writing.
If you use Notepad++, install the plugin called TextFX.
- Replace all occurances of rn with blank.
- Select all the text
- Use the new menu TextFX -> TextFX Edit -> E:Delete Blank Lines
I hope this helps.
add a comment |
You cant write the file you are currently reading in. Also you are stripping things via file.splitlines()
from each line - this way you'll remove all rn
- not only those in empty lines. Store content in a new name and delete/rename the file afterwards:
Create demo file:
with open ("t.txt","w") as f:
f.write("""
asdfb
adsfoine
""")
Load / create new file from it:
with open("t.txt", 'r') as r, open("q.txt","w") as w:
for l in r:
if l.strip(): # only write line if when stripped it is not empty
w.write(l)
with open ("q.txt","r") as f:
print(f.read())
Output:
asdfb
adsfoine
( You need to strip()
lines to see if they contain spaces and a newline. )
For rename/delete see f.e. How to rename a file using Python and Delete a file or folder
import os
os.remove("t.txt") # remove original
os.rename("q.txt","t.txt") # rename cleaned one
add a comment |
It's nice and easy...
file_path = "C:\Users\ryans\OneDrive\Desktop\output\*.yaml"
with open(file_path,"r+") as file:
lines = file.readlines()
file.seek(0)
for i in lines:
if i.rstrip():
file.write(i)
Where you open the file, read the lines, and if they're not blank write them back out again.
which will accomplish nothing because the offending lines consist of spaces AND'rn'
– Patrick Artner
Nov 21 '18 at 15:47
You're right. If you remove rn then it works perfectly...
– Richard Roberts
Nov 21 '18 at 15:52
This worked for me. Thanks Richard, and everyone else too.
– ryguy72
Nov 21 '18 at 16:05
Happy to help! If you're willing to mark one of these answers as accepted, it'll help people focus on other SO problems without answers.
– Richard Roberts
Nov 21 '18 at 16:19
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415329%2fhow-to-delete-blank-lines-in-a-text-file-on-a-windows-machine%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you use python, you can update the line using:
re.sub(r'[srn]','',line)
Close the reading file handler before writing.
If you use Notepad++, install the plugin called TextFX.
- Replace all occurances of rn with blank.
- Select all the text
- Use the new menu TextFX -> TextFX Edit -> E:Delete Blank Lines
I hope this helps.
add a comment |
If you use python, you can update the line using:
re.sub(r'[srn]','',line)
Close the reading file handler before writing.
If you use Notepad++, install the plugin called TextFX.
- Replace all occurances of rn with blank.
- Select all the text
- Use the new menu TextFX -> TextFX Edit -> E:Delete Blank Lines
I hope this helps.
add a comment |
If you use python, you can update the line using:
re.sub(r'[srn]','',line)
Close the reading file handler before writing.
If you use Notepad++, install the plugin called TextFX.
- Replace all occurances of rn with blank.
- Select all the text
- Use the new menu TextFX -> TextFX Edit -> E:Delete Blank Lines
I hope this helps.
If you use python, you can update the line using:
re.sub(r'[srn]','',line)
Close the reading file handler before writing.
If you use Notepad++, install the plugin called TextFX.
- Replace all occurances of rn with blank.
- Select all the text
- Use the new menu TextFX -> TextFX Edit -> E:Delete Blank Lines
I hope this helps.
answered Nov 21 '18 at 15:39
pratik mankarpratik mankar
7119
7119
add a comment |
add a comment |
You cant write the file you are currently reading in. Also you are stripping things via file.splitlines()
from each line - this way you'll remove all rn
- not only those in empty lines. Store content in a new name and delete/rename the file afterwards:
Create demo file:
with open ("t.txt","w") as f:
f.write("""
asdfb
adsfoine
""")
Load / create new file from it:
with open("t.txt", 'r') as r, open("q.txt","w") as w:
for l in r:
if l.strip(): # only write line if when stripped it is not empty
w.write(l)
with open ("q.txt","r") as f:
print(f.read())
Output:
asdfb
adsfoine
( You need to strip()
lines to see if they contain spaces and a newline. )
For rename/delete see f.e. How to rename a file using Python and Delete a file or folder
import os
os.remove("t.txt") # remove original
os.rename("q.txt","t.txt") # rename cleaned one
add a comment |
You cant write the file you are currently reading in. Also you are stripping things via file.splitlines()
from each line - this way you'll remove all rn
- not only those in empty lines. Store content in a new name and delete/rename the file afterwards:
Create demo file:
with open ("t.txt","w") as f:
f.write("""
asdfb
adsfoine
""")
Load / create new file from it:
with open("t.txt", 'r') as r, open("q.txt","w") as w:
for l in r:
if l.strip(): # only write line if when stripped it is not empty
w.write(l)
with open ("q.txt","r") as f:
print(f.read())
Output:
asdfb
adsfoine
( You need to strip()
lines to see if they contain spaces and a newline. )
For rename/delete see f.e. How to rename a file using Python and Delete a file or folder
import os
os.remove("t.txt") # remove original
os.rename("q.txt","t.txt") # rename cleaned one
add a comment |
You cant write the file you are currently reading in. Also you are stripping things via file.splitlines()
from each line - this way you'll remove all rn
- not only those in empty lines. Store content in a new name and delete/rename the file afterwards:
Create demo file:
with open ("t.txt","w") as f:
f.write("""
asdfb
adsfoine
""")
Load / create new file from it:
with open("t.txt", 'r') as r, open("q.txt","w") as w:
for l in r:
if l.strip(): # only write line if when stripped it is not empty
w.write(l)
with open ("q.txt","r") as f:
print(f.read())
Output:
asdfb
adsfoine
( You need to strip()
lines to see if they contain spaces and a newline. )
For rename/delete see f.e. How to rename a file using Python and Delete a file or folder
import os
os.remove("t.txt") # remove original
os.rename("q.txt","t.txt") # rename cleaned one
You cant write the file you are currently reading in. Also you are stripping things via file.splitlines()
from each line - this way you'll remove all rn
- not only those in empty lines. Store content in a new name and delete/rename the file afterwards:
Create demo file:
with open ("t.txt","w") as f:
f.write("""
asdfb
adsfoine
""")
Load / create new file from it:
with open("t.txt", 'r') as r, open("q.txt","w") as w:
for l in r:
if l.strip(): # only write line if when stripped it is not empty
w.write(l)
with open ("q.txt","r") as f:
print(f.read())
Output:
asdfb
adsfoine
( You need to strip()
lines to see if they contain spaces and a newline. )
For rename/delete see f.e. How to rename a file using Python and Delete a file or folder
import os
os.remove("t.txt") # remove original
os.rename("q.txt","t.txt") # rename cleaned one
edited Nov 21 '18 at 15:44
answered Nov 21 '18 at 15:38
Patrick ArtnerPatrick Artner
25.3k62444
25.3k62444
add a comment |
add a comment |
It's nice and easy...
file_path = "C:\Users\ryans\OneDrive\Desktop\output\*.yaml"
with open(file_path,"r+") as file:
lines = file.readlines()
file.seek(0)
for i in lines:
if i.rstrip():
file.write(i)
Where you open the file, read the lines, and if they're not blank write them back out again.
which will accomplish nothing because the offending lines consist of spaces AND'rn'
– Patrick Artner
Nov 21 '18 at 15:47
You're right. If you remove rn then it works perfectly...
– Richard Roberts
Nov 21 '18 at 15:52
This worked for me. Thanks Richard, and everyone else too.
– ryguy72
Nov 21 '18 at 16:05
Happy to help! If you're willing to mark one of these answers as accepted, it'll help people focus on other SO problems without answers.
– Richard Roberts
Nov 21 '18 at 16:19
add a comment |
It's nice and easy...
file_path = "C:\Users\ryans\OneDrive\Desktop\output\*.yaml"
with open(file_path,"r+") as file:
lines = file.readlines()
file.seek(0)
for i in lines:
if i.rstrip():
file.write(i)
Where you open the file, read the lines, and if they're not blank write them back out again.
which will accomplish nothing because the offending lines consist of spaces AND'rn'
– Patrick Artner
Nov 21 '18 at 15:47
You're right. If you remove rn then it works perfectly...
– Richard Roberts
Nov 21 '18 at 15:52
This worked for me. Thanks Richard, and everyone else too.
– ryguy72
Nov 21 '18 at 16:05
Happy to help! If you're willing to mark one of these answers as accepted, it'll help people focus on other SO problems without answers.
– Richard Roberts
Nov 21 '18 at 16:19
add a comment |
It's nice and easy...
file_path = "C:\Users\ryans\OneDrive\Desktop\output\*.yaml"
with open(file_path,"r+") as file:
lines = file.readlines()
file.seek(0)
for i in lines:
if i.rstrip():
file.write(i)
Where you open the file, read the lines, and if they're not blank write them back out again.
It's nice and easy...
file_path = "C:\Users\ryans\OneDrive\Desktop\output\*.yaml"
with open(file_path,"r+") as file:
lines = file.readlines()
file.seek(0)
for i in lines:
if i.rstrip():
file.write(i)
Where you open the file, read the lines, and if they're not blank write them back out again.
edited Nov 21 '18 at 15:52
answered Nov 21 '18 at 15:45
Richard RobertsRichard Roberts
84
84
which will accomplish nothing because the offending lines consist of spaces AND'rn'
– Patrick Artner
Nov 21 '18 at 15:47
You're right. If you remove rn then it works perfectly...
– Richard Roberts
Nov 21 '18 at 15:52
This worked for me. Thanks Richard, and everyone else too.
– ryguy72
Nov 21 '18 at 16:05
Happy to help! If you're willing to mark one of these answers as accepted, it'll help people focus on other SO problems without answers.
– Richard Roberts
Nov 21 '18 at 16:19
add a comment |
which will accomplish nothing because the offending lines consist of spaces AND'rn'
– Patrick Artner
Nov 21 '18 at 15:47
You're right. If you remove rn then it works perfectly...
– Richard Roberts
Nov 21 '18 at 15:52
This worked for me. Thanks Richard, and everyone else too.
– ryguy72
Nov 21 '18 at 16:05
Happy to help! If you're willing to mark one of these answers as accepted, it'll help people focus on other SO problems without answers.
– Richard Roberts
Nov 21 '18 at 16:19
which will accomplish nothing because the offending lines consist of spaces AND
'rn'
– Patrick Artner
Nov 21 '18 at 15:47
which will accomplish nothing because the offending lines consist of spaces AND
'rn'
– Patrick Artner
Nov 21 '18 at 15:47
You're right. If you remove rn then it works perfectly...
– Richard Roberts
Nov 21 '18 at 15:52
You're right. If you remove rn then it works perfectly...
– Richard Roberts
Nov 21 '18 at 15:52
This worked for me. Thanks Richard, and everyone else too.
– ryguy72
Nov 21 '18 at 16:05
This worked for me. Thanks Richard, and everyone else too.
– ryguy72
Nov 21 '18 at 16:05
Happy to help! If you're willing to mark one of these answers as accepted, it'll help people focus on other SO problems without answers.
– Richard Roberts
Nov 21 '18 at 16:19
Happy to help! If you're willing to mark one of these answers as accepted, it'll help people focus on other SO problems without answers.
– Richard Roberts
Nov 21 '18 at 16:19
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415329%2fhow-to-delete-blank-lines-in-a-text-file-on-a-windows-machine%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I am afraid that you cannot read and write to the same file at the same time without using seek(). Usually the better way is to create a temporary file, write the change there and then replace the original one (or rename the original to .backup or something). That way if the program fails in the middle you still have a consistent state. Otherwise it should be enough to add
if line != '':
after therstrip()
. So you really skip the empty lines.– petrch
Nov 21 '18 at 15:34