tags:

views:

199

answers:

5

UNIX absolute path starts with '/', whereas Windows starts with alphabet 'C:' or '\'. Does python has a standard function to check if a path is absolute or relative? Or do I have to come up with a function using regular expression?

+2  A: 

Use os.path.isabs.

KennyTM
+4  A: 

os.path.isabs returns True if the path is absolute, False if not. The documentation says it works in windows (I can confirm it works in Linux personally).

os.path.isabs(my_path)
orangeoctopus
+1  A: 

The os.path module will have everything you need.

Rob Lourens
+1  A: 
import os.path

os.path.isabs('\home\user')
True

os.path.isabs('user')
False
Alex Bliskovsky
+1  A: 

And if what you really want is the absolute path, don't bother checking to see if it is, just get the abspath:

import os

print os.path.abspath('.')
Wayne Werner