Verify that the string is in absolute format

I have a line that contains user input for a directory address on a linux system. I need to check if it is formatted correctly and can be an address in Python 2.6. It is important to note that this is not on the current system, so I cannot check if it exists using os.path, and I cannot try to create directories, since the function will be executed many times.

These lines will always be absolute paths, so my first thought was to look for a leading slash. From there, I wondered if the rest of the string only checks for valid characters and does not contain any double slashes. It seems a bit awkward, any other ideas?

+7
source share
1 answer

I am sure that the question has been edited since the moment of writing, but:

There is os.path.isabs (PATH) that will tell you if the path is absolute or not.

Returns True if the path is an absolute path. On Unix, this means that it starts with a slash on Windows when it starts with a (back) slash after shredding a potential drive letter.

+19
source

All Articles