Python3 - How to get back folder?

Asked

Viewed 702 times

2

My file main py. is in the root directory of my project, example:

"C:\Projetos\PyValidacao\main.py"

With the module os I can take the real path of main py., but I need to make you create a folder inside "C:\Projetos". The problem is that I can’t get it back to the folder from the root of the project.

How to get back the directory with the module os ? There’s something better for that purpose ?

1 answer

3

I was able to solve using the "abspath" function as follows:

return os.path.abspath(r"..\")

That way it returned a directory as I wanted.

  • I was just about to say, "." That’s cool,!

  • Cleber, remember that the "\" serves for special characters. Then put another bar to not cause problems in your code.

  • 2

    @Jeanextreme002 with prefix r string is not required

  • I didn’t see the kkk Sorry prefix

Browser other questions tagged

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