Iván Renison
Iván Renison

Reputation: 51

Convert filepath string to python import string

I would like to convertos file paths like './foo/bar/file.py' to paths used in python import like 'foo.bar.file'.

I could very easily code my self these functions, but I wanted to know if there is a function for making that?

It would be great also if the functions works for windows and linux file paths

Upvotes: 0

Views: 50

Answers (1)

Javad
Javad

Reputation: 2098

try this:

import pathlib

path = pathlib.Path('foo/bar/file.py')
formatted_path = '.'.join(path.with_suffix('').parts)

# And do what you want with formatted_path

Upvotes: 2

Related Questions