Search code examples
pythonlistdir

How can I put '..' in the paths that we put in the os.listdir parameter?


I use the os.listdir function to (as you can imagine) list the folders and files I have in a folder. As the documentation says, "It does not include the special entries '.' and '..' even if they are present in the directory.".

I have a problem with this because my code is meant to be published on GitHub and I don't want everyone to see my entire path and besides, since they don't have the same path, the code doesn't work for them.

So I'm wondering how to get around that.

Thanks!

PS: my folder looks like this: folder. The python code is in the py folder and the folders and files I want to access to are in the Dico folder.


Solution

  • You've misunderstood the documentation. The documentation is saying that the special entries '.' and '..' will not show up in the output of os.listdir.

    The documentation is not saying that anything special or unusual happens if the argument to os.listdir includes ... You don't need to use absolute paths or any other workaround. If you want a list of the contents of a directory named Dico adjacent to the working directory, you can just use

    os.listdir('../Dico')