How to split a dos path into its components in Python

I have a string variable which represents a dos path e.g:

var = "d:\stuff\morestuff\furtherdown\THEFILE.txt"

I want to split this string into:

[ "d", "stuff", "morestuff", "furtherdown", "THEFILE.txt" ]

I have tried using split() and replace() but they either only process the first backslash or they insert hex numbers into the string.

I need to convert this string variable into a raw string somehow so that I can parse it.

What’s the best way to do this?

I should also add that the contents of var i.e. the path that I’m trying to parse, is actually the return value of a command line query. It’s not path data that I generate myself. Its stored in a file, and the command line tool is not going to escape the backslashes.

23 Answers
23

Leave a Comment