0

I have a list containing directories: ("..\b\c.txt", "..\b\d.txt"). I was planning to use .split() method to have another list of the directory components separated by "\", but I'm not having success mostly due to "\" usage. Thus, the final result will be something like this ("a", "b, "c.txt") ...

I do understand \ being a escape character and the difficulties reported by this link, but I'm not understanding if it's possible to convert the entire list to raw string. Can anyone help?

files = ('..\..\data\User\level1\level2\sun.png','..\..\data\User\level1\level2\description.txt')

I was testing something like this:

str = files[0].split("\")

Desired output: a list containing:

('data', 'User', 'level1', 'level2', 'sun.jpg')
Community
  • 1
  • 1
dudas
  • 403
  • 6
  • 23
  • 1
    Please show us your actual code, so we can fix any mistakes you have have made in it. Note that the [`os.path` module](https://docs.python.org/2/library/os.path.html) can do just about all could want when it comes to path handling, however. – Martijn Pieters Feb 13 '15 at 21:40

2 Answers2

3

If files is a literal, use raw literal notation then:

files = (
    r'..\..\data\User\level1\level2\sun.png',
    r'..\..\data\User\level1\level2\description.txt'
)

In this specific example, you managed to evade all of the known character escape sequences, but that was just lucky.

You can use "\\" to define a string with just a backslash:

files[0].split("\\")

Demo:

>>> files = (
...     r'..\..\data\User\level1\level2\sun.png',
...     r'..\..\data\User\level1\level2\description.txt'
... )
>>> files[0].split("\\")
['..', '..', 'data', 'User', 'level1', 'level2', 'sun.png']

If you wanted to split a path into parts, you probably should look at os.path.split() and the other functions in that module. Using such functions helps keep your code platform agnostic.

There is also os.sep and os.altsep, which would also help keep your code indepedendent of the OS.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks I will look onto that. Regarding your first suggestion the problem is that the "files" list contains over 1000 paths, is there any way to make the raw literal notation to all the list? – dudas Feb 13 '15 at 22:03
  • No; string literals are parsed as you produce them; once the object has been created it is really too late to repair the damage. – Martijn Pieters Feb 13 '15 at 22:11
  • Note that raw string literals are *just a notation*. They produce string object values just like regular string literals. Only the details of the notation differ. – Martijn Pieters Feb 13 '15 at 22:17
2

It turns out you're not quite there yet with understanding \ as an escape character. The escape characters are not extra characters in the actual string, they're extra characters in the source code.

To create a string that contains one \ character in Python, you can use \ as an escape character like this:

"\\"

There are 2 characters between the quotes in the source code, but in the actual string, there is only the "escaped character", which is the second \.

Note that "raw strings" are similar: the difference is in the source code, not the actual string. The following are equal:

"abc\\def"
r"abc\def"

What I do when I have doubts about things like this is play around in the interactive mode of the interpreter, and I'd suggest you do the same. For example, if you were unsure of what the real contents of a string like "abc\\def" really are, you could try print-ing it.

Dan Getz
  • 8,774
  • 6
  • 30
  • 64