Python3: How to treat relative paths with module os

Asked

Viewed 38 times

0

I’ve got it down:

def get_conteudo_file(filename: str, encoding: str="iso8859-1") -> str:
    filename = os.path.abspath(os.path.join(*filename.split(os.sep)))
    assert os.path.isfile(filename), "Arquivo '{}' indisponível".format(filename)
    with open(filename, encoding=encoding) as f:
        logging.debug('filename: "{}" readed'.format(f.name))
        return f.read()

get_conteudo_file("~/Documents/projeto/me.py", "utf-8")

How to treat this relative path to access the file?

  • 1

    What was the problem? He did not locate the file? What was the final value of filename?

  • It was something like "/home/Brito/projetos/este_projeto/me/~/Documents/projeto/me.py"

  • See if this helps you: https://answall.com/a/335516/5878

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.