Abdul Ahmad
Abdul Ahmad

Reputation: 10019

python abspath returning path twice

I'm trying to get an absolute path of a relative path string with python but it keeps printing the path twice. For example:

self.path = 'Users/abdulahmad/Desktop'
self.actual_path = os.path.abspath(self.path)
print self.actual_path

my console prints

/Users/abdulahmad/Desktop/Users/abdulahmad/Desktop

and if I change the path to:

self.path = 'Desktop'

my console prints:

/Users/abdulahmad/Desktop/Desktop

shouldn't it just print /Users/abdulahmad/Desktop in both cases?

Upvotes: 1

Views: 852

Answers (1)

skyking
skyking

Reputation: 14400

Probably because the current working directory is /Users/abdulahmad/Desktop.

If you write for example path/to/file it means relative to current working directory and relative to /Users/abdulahmad/Desktop it would mean /Users/abdulahmad/Desktop/path/to/file.

If you read the python3 manual it actually shows an implementation of os.abspath(path) as being the same as os.path.normpath(os.path.join(os.getcwd(), path)). This can be used to get a path relative to arbitrarily provided path. (It also shows that you actually basically joins the current working directory and the supplied (relative) path)

Upvotes: 3

Related Questions