Use your original string, but instead of quoting it with " in front and " at the end, use r''' and '''
The ''' form is called
triple quoting, and it takes another ''' to end the quote (much more unlikely to come up in your string than a single ' or "). You can use either single or double quote characters for this (so, ''' or """), they operate the same. The
r before the first ''' makes it a
raw string, which means that Python won't try to treat the embedded backslash in any special way.
And, if the contents of the file are something you're going to subsequently read into your Python script, there are much better (safer and more efficient) ways to do this:
Code: Select all
#!/usr/bin/env python3
import subprocess
import re
command = ['sudo', 'iwlist', 'wlan0', 'scan']
for line in subprocess.check_output(command).splitlines():
m = re.match(r'\s*ESSID:"(.*)"', line.decode())
if m:
essid = m.group(1)
print(essid)