-1

I got a script the pipes the output of the find command to a file. I then open the file and find a line that contains a set of characters. I then convert the line to a string and use the replace method to remove the leading and ending [' and ']. I want to assign the line to a variable so I can use it in a subsequent command but there is always a trailing \n for a newline no matter what I do. Does anyone know how I could remove the trailing '\n'? I've tried using the replace and rstrip methods without any luck.

cmdline("find / -name php.ini >> temp.out")

st = open('temp.out', 'r')
for line in st:
        line = line.rstrip()
st.close()

with open('temp.out') as php_ini:
        for num, line in enumerate(php_ini, 1):
                if 'apache2' in line:
                        content = php_ini.readlines(num)
                        contentStr = str(content).replace("['", "")
                        contentStrTwo = contentStr.replace("']", "")
                        print (contentStrTwo)
game0ver
  • 1,250
  • 9
  • 22

1 Answers1

1

You can make that a bit simpler with just this:

with open('temp.out') as php_ini:
    for num, line in enumerate(php_ini, 1):
        if 'apache2' in line:
            content = line.rstrip('\n').replace("['", "").replace("']", "")
            print (content)
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135