Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python string ' " ' : single double quote inside string

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"') 
like image 423
user442920 Avatar asked Nov 30 '12 19:11

user442920


2 Answers

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.

like image 124
NPE Avatar answered Oct 24 '22 01:10

NPE


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))
like image 37
Sebastian Bartos Avatar answered Oct 24 '22 01:10

Sebastian Bartos