MKT97
MKT97

Reputation: 23

Converting os.path to pathlib module no working out as expected

Ive been trying to substitute the os.path with the new pathlib module. In these lines the os.path works every time with no error, while the Pathlib module brings the error:

Path' and 'str'
[!] send_logs // Error.. ~ unsupported operand type(s) for +: 'PosixPath' and 'str'

The main purpose of this is to write a file to that folder; using Path.is_dir(log_dir) comes back True. Attempting this though through the pathlib module brings the error. Ive tried to locate other sources for an answer and came to the PosixPath as a separate variable to be used in expanduser; to no avail I am brought here

Im sorry in advanced as i am novice and this is one of my first 'projects'! All the help is greatly appreciated. I have also come across similar questions but they were directed at finding the home dir, i.e Path.owner(Path.home())

#log_dir = os.path.expanduser('~') + '/Downloads/'   --- commented out for pathlib/path
p = PosixPath('~' + '/Downloads/')
log_dir = Path.expanduser(Path(p))

Thanks for the help that was given. Managed to get it to work with a single line: in the same manner as os.path.expanduser()

log_dir = str(PosixPath('~' + '/Downloads/').expanduser())

Upvotes: 2

Views: 1012

Answers (1)

kleerofski
kleerofski

Reputation: 423

EDIT: I think you're looking for this:

>>> p = PosixPath('~/films/Monty Python')
>>> p.expanduser()
PosixPath('/home/eric/films/Monty Python')

keep in mind that pathlib.Path(somepath) returns a pathlib object, but not string. To concatenate the pathlib object to string, use either

str(pathlib.Path(somepath))+"somepath"

or look for .str or similar method in pathlib documentation

Upvotes: 1

Related Questions