photon
12/31/2016 - 8:04 AM

remove blank space or new lines for the given string

remove blank space or new lines for the given string

#!/usr/bin/python

# Works at least for python 2.7.3

import sys
import os
import re

# testStr is for test usage, when they are assinged to the variable s in main() below
testStr1="a b\n c \n d "
#testStr2='/path/to/white_space/file'

def doStr(s):
    pattern = re.compile(r'\s+')
    strg = re.sub(pattern, '', s)
    print 'Ridded result:\n', strg

def doFile(fPath):
    with open(fPath, 'r') as f:
        readed = [line.rstrip() for line in f]
    strReaded = ''.join(readed)
    print strReaded
    doStr(strReaded)

def main():
    print 'Args: ',str(sys.argv) 
    #s = testStr1
    s = ''
    if len(sys.argv) > 1:
        s=sys.argv[1]

    if os.path.isfile(s):
        doFile(s)
    elif len(s) > 0:
        doStr(s)
    else:
        print "Nothing I can do"

main()