Created
July 16, 2018 15:47
-
-
Save plgonzalezrx8/d876de4592207a0ada0548afb508e499 to your computer and use it in GitHub Desktop.
txt file cleaner to remove all empty lines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python2.7 | |
import glob | |
counter = 0 | |
cache = [] | |
# Get rid of empty lines | |
def cleanemptylines(filename): | |
print "Now cleaning the text file for whitespaces" | |
print "Current file is %s " % (filename) | |
# Get file contents | |
fd = open(filename) | |
contents = fd.readlines() | |
fd.close() | |
new_contents = [] | |
for line in contents: | |
# Strip whitespace, should leave nothing if empty line was just "\n" | |
if not line.strip(): | |
continue | |
# We got something, save it | |
else: | |
new_contents.append(line) | |
# make new file without empty lines | |
fd = open(filename, "w") | |
fd.write("".join(new_contents)) | |
print "succesfully done" | |
fd.close() | |
print "Cleaning Complete" | |
#this line will gather all the txt files in the directory, might need some tweaking to work in your environment. | |
txt_file_list = glob.glob("*.txt") | |
#this will clean all the txt files in the current directory. | |
#By cleaning I mean removing all the spaces and unsupported symbols. | |
for i in txt_file_list: | |
cleanemptylines(i) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment