0

Is any easy way to quickly initialize path format to list variables in python Such as:

dir = "/root/path/file"

to

p[0] = "root"
p[1] = "path"
p[3] = "file"
gtlambert
  • 11,253
  • 2
  • 28
  • 47
Robber Pen
  • 927
  • 2
  • 8
  • 21

2 Answers2

0

You can try like this,

>>> dir="/root/path/file"
>>> p = filter(lambda x: x != '', dir.split('/'))
>>> print(p)
['root', 'path', 'file']
Adem Öztaş
  • 18,635
  • 4
  • 32
  • 41
  • This is a better solution than mine for the *exact* example posted, but will not work for something like `"path/subpath/file"` – gtlambert Mar 03 '16 at 15:18
0

You can use split function for this:

p = dir.split('/')
gtlambert
  • 11,253
  • 2
  • 28
  • 47
Alex
  • 1,083
  • 7
  • 13