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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like