2

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'

:)

2
  • 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, 2013 at 14:37
  • I tried, wasn't able to answer it :/
    – GEOGEO
    Apr 18, 2013 at 10:30

1 Answer 1

4

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

1
  • 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, 2013 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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