-3

I'm writing some code that uses an Onclick event to get some file paths. I need to make sure these file paths are literals to make sure they're correct so the rest of my code can run. Right now I think I'm getting the file paths as unicode. Essentially I need this:

u"File\location\extra\slash"

to be this:

r"File\location\extra\slash"

How do I do this? I haven't been able to find anyone who's actually been able to do this successfully and the documentation doesn't have any examples of this. I can't change how the function that gives me the file paths Onclick event works.

Here is the code in question:

class SetLayer(object):
    """Implementation for leetScripts_addin.button2 (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        self.a = pythonaddins.GetSelectedCatalogWindowPath()
        print self.a
        #code split up path here
        self.b = os.path.split(str(self.a))
        self.c = self.b[0]
        self.d = os.path.split(self.c)
        self.e = (self.b[1])
        self.f = (self.d[1])
        self.g = (self.d[0])
  • 3
    Refer this: http://stackoverflow.com/questions/7262828/python-how-to-convert-string-literal-to-raw-string-literal – pokemon Jul 01 '16 at 22:50
  • 2
    It sounds like you're mistaken about what you actually have, if you say you have `u"File\location\extra\slash"`. Also, unless you actually want to print output with an "r" on the front and double quotes, what you need isn't actually a string literal. Can you `print repr(your_current_string)` and show us what that displays? – user2357112 Jul 01 '16 at 22:51
  • isn't it just u"File\location\extra\slash".decode('utf-8') ? – dmitryro Jul 01 '16 at 22:57
  • Can't check right now, but I believe `os.path.join(yourPath)` should produce a valid platform dependent path – scope Jul 01 '16 at 22:58
  • 1
    after rereading, if you just need raw string to pass it somewhere else, this should work: `yourPath.encode('string-escape')` – scope Jul 01 '16 at 23:01
  • 2
    your question does not make sense ... please provide actual code and actual output ... vs what you hope the output was or why you think it is incorrect – Joran Beasley Jul 01 '16 at 23:09
  • @JoranBeasley I did check and I was getting u"File\location\extra\slash" as output. I wanted the string to act like r"File\location\extra\slash" and didn't know how else to phrase the question. I'm editing my question to show some more code now. –  Jul 06 '16 at 21:10
  • @user2357112 that returns "u'File\\\\location\\\\extra\\\\slash" I don't understand why the u is part of that string. –  Jul 06 '16 at 21:17
  • @Steve: Code format it, please, so the backslashes display correctly. The `u` means it's a Unicode string. – user2357112 Jul 06 '16 at 21:29
  • @scope That returns "File\\location\\extra\\slash" –  Jul 06 '16 at 21:35
  • @user2357112 there is actually four slashes per slash in the output there. `"u'File\\\\location\\\\extra\\\\slash"` –  Jul 06 '16 at 21:36
  • @Steve `r"File\location\extra\slash"` returns `"File\\location\\extra\\slash"` as well, right? – scope Jul 06 '16 at 21:40
  • @scope It does not. `r"File\location\extra\slash"` returns `File\location\extra\slash` –  Jul 06 '16 at 21:43
  • @Steve: Is that the output for `print repr(self.a)`, or for one of the later things in your code, or something else? The `"u'File\\\\location\\\\extra\\\\slash"` output you're getting suggests that you've accidentally mangled your string in several ways; it looks like your string is a bytestring containing most of the `repr` of a Unicode string, where the Unicode string had twice as many backslashes as it should. – user2357112 Jul 06 '16 at 21:43
  • @user2357112 I did this `self.b = os.path.split(repr(self.a))` and the output for `print self.g` was that mangled output. How do I fix this? What am I doing wrong with my strings? –  Jul 06 '16 at 21:50
  • @Steve: Show us the output of `print repr(self.a)`, and tell us what information you're trying to extract from that string. – user2357112 Jul 06 '16 at 22:00
  • @user2357112 It's `u'File\\location\\extra\\slash'` I'm trying to get `e = 'slash' f = 'extra' g = 'File\location'` –  Jul 06 '16 at 22:04

2 Answers2

0

From your comments, you have a = u'File\\location\\extra\\slash', and you want to extract e = 'slash', f = 'extra', and g = 'File\location'. Nothing here requires converting strings to string literals; you've just gotten really confused by various levels of string escaping.

You'll need to decide whether e, f, and g should be Unicode strings or bytestrings. Unicode strings are probably the right choice, but I can't make that choice for you. Whatever you choose, you'll need to make sure you know whether you're dealing with Unicode strings or bytestrings at all times. Currently, a is a Unicode string.

If you want Unicode strings for e, f, and g, you can do

self.e, temp = os.path.split(self.a)
self.g, self.f = os.path.split(temp)

If you want bytestrings, you'll need to encode self.a with the appropriate encoding and then do the above os.path.split calls. What the appropriate encoding is will depend on your specific OS and application. sys.getfilesystemencoding() or 'utf-8' are likely choices.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Where am I string escaping? –  Jul 07 '16 at 21:05
  • @Steve: The code you posted doesn't do any escaping, but raw string syntax auto-escapes backslashes, and `u"File\location\extra\slash"` looks like you mixed up two different levels of escaping manually. You also used `repr` wrong in the comments the first time (`repr` does some escaping; it's actually the closest thing to "convert a string to a string literal" that Python has, though that's not what you really need.) – user2357112 Jul 07 '16 at 21:10
  • How did I use it wrong? Is there a resource that I could use to learn more about how strings actually work? I wasn't aware that it made a difference. –  Jul 07 '16 at 21:16
-2

You can use eval.

MacBookPro:~ DavidLai$ python
Python 2.7.11 (default, Jan 22 2016, 08:29:18)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = u"File\location\extra\slash"
>>> y = eval("r\"" + x + "\"")
>>> y
'File\\location\\extra\\slash'
>>> type(y)
<type 'str'>
>>>
David Lai
  • 814
  • 7
  • 13
  • whoa nelly .... we dont need to go jump off a cliff yet ... (as an aside your solution would not work if the path was `"File\next\location"`) – Joran Beasley Jul 01 '16 at 23:07
  • 2
    evaluating string that comes from js (eg web client side and probably untrusted users) is a really bad idea. – Ronen Ness Jul 01 '16 at 23:08
  • 2
    `eval('os.system('rm -rf /something/important')")` and this is just one random thing which could happen – scope Jul 01 '16 at 23:10