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,711
  • 2
  • 30
  • 48
Robber Pen
  • 1,033
  • 2
  • 12
  • 24

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ş
  • 20,457
  • 4
  • 34
  • 42
  • 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,711
  • 2
  • 30
  • 48
Alex
  • 1,141
  • 8
  • 13