Reputation: 847
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"')
Upvotes: 6
Views: 7145
Reputation: 2497
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))
Upvotes: 2
Reputation: 27575
The backslash after upe
is escaping the first single quote closing:
'copy "C:\upe\' + logA + '"' + ' "C:\upe\log.txt"'
--------------^ add an escape at least here, and it will work!
Another option is to compose your command in more steps:
basepath = r'C:\upe'
inpath = os.path.join(basepath, logA)
outpath = os.path.join(basepath, 'log.txt')
logA = 'thing.txt'
command = 'copy "%s" "%s"' % (inpath, outpath)
print command
cmdpipe = os.popen(command)
Upvotes: 0
Reputation: 500367
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.
Upvotes: 3