A double quote looks like this ". If I put this inside a python string I get this ' " '
. In python, I can put two double quotes in a string ' "" '
and this gets printed as two double quotes. However, I cannot put a single double quote in a string, as before, ' " '
. I am doing this in eclipse with pydev and it gives an error: "encountered "\r" (13), after : ""
. I am trying to do the following with command pipe and file names:
logA = 'thing.txt'
cmdpipe = os.popen('copy "C:\upe\' + logA + '"' + ' "C:\upe\log.txt"')
You need to escape the backslashes:
logA = 'thing.txt'
cmdpipe = os.popen('copy "C:\\upe\\' + logA + '"' + ' "C:\\upe\\log.txt"')
Typically, one would use raw strings (r'...'
) when there are backslashes inside a string literal. However, as pointed out by @BrenBarn, this won't work in this case.
You need to escape the backslashes, otherwise it will do odd things.
logA = 'thing.txt'
cmdpipe = os.popen(
'copy "C:\\upe\\' + logA + '"' + ' "C:\\upe\\log.txt"')
Edit: A more pythonic way would be this though:
logA = 'thing.txt'
cmdpipe = os.popen('copy "C:\\upe\\{}" "C:\\upe\\log.txt"'.format(logA))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With