Python Path as a String

I am specifying a relative file path using jacaro's path module.

How can I get the absolute path from this variable as a string?

import path # From 

path = path.path('~/folder/')
relative_filename = path.joinpath('foo')
# how can I get the absolute path of as a string?
absolute_path = ???
fd = open(absolute_path)
3

1 Answer

filename has a method called abspath that returns an object with the absolute path. You can cast that to a string.

...
folder_path = path('~/folder/')
filename = path.joinpath('foo')
f = sftp.open(str(filename), 'r')
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest