Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I want to calculate a field, this is my working example in VB:

"C:\Users\Admin\Desktop\Data\" & !Work! & "\1000_\" & !Data! & ".pdf"

I'm slowly changing all my scripts to Python, how would I write this in Python?


got it:

'C:\\Users\\Admin\\Desktop\\Data\\'!Work!'\\1000_\\'!Data!'.pdf'

:)

share|improve this question
2  
I suggest you post your edit ("got it: ...") as an answer, then accept either your answer or Clickinaway's. That way future readers with the same problem will quickly see how to solve it. –  user3461 Apr 12 '13 at 14:37
    
I tried, wasn't able to answer it :/ –  GEOGEO Apr 18 '13 at 10:30

1 Answer 1

up vote 4 down vote accepted

You could also use 'r' before your path reference OR use '/' to avoid the need for '\'

For example: r"C:\Users\Admin\Desktop\Data\"

OR

"C:/Users/Admin/Desktop/Data/"

Programming languages, such as Python, treat a backslash () as an escape character. For instance, \n represents a line feed, and \t represents a tab. When specifying a path, a forward slash (/) can be used in place of a backslash. Two backslashes can be used instead of one to avoid a syntax error. A string literal can also be used by placing the letter r before a string containing a backslash so it is interpreted correctly.

Setting paths to data in Python

share|improve this answer
1  
r"C:\Users\Admin\Desktop\Data\" raises a SyntaxError, so it actually needs to be r"C:\Users\Admin\Desktop\Data\"" –  Mike T Nov 25 '13 at 11:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.