Python get current directory

Introduction

Today, we are going to talk about directories, files and paths in Python. If you are in rush and only need to know the syntax, here is how to get the current working directory…

For more information, continue reading…

Python provides dedicated cross platform modules to interface with the operating system and file system. In particular, we are going to talk about the os and os.path modules…

  • os module provides a portable interface to work with operating system dependent functionality
  • os.path module is used to manipulate file system paths

Will start with the __file__ constant…

__file__ constant

Contains file name of the currently executing Python script. It depends on how the script was called (ex. python ./test.py) regardless of the location from which it was invoked.

Let us take an example…

  • Open a text file and give it a name (ex. test.py)
  • Add the following code

  • Run the script as follows…

  • You should get the following output…

As you can see, it printed the Python script name the way it was invoked.

os.path.realpath()

Returns the full path to a directory or file. It eliminates symbolic links in the path. Here is an example…

  • Create two directories dir1 and dir2

  • Create a symbolic link for dir2 inside dir1

  • Open a text file and give it a name (ex. test.py)
  • Copy the following code to the script

  • Save the python script in dir2
  • Go to dir1/dir2 and run the script…

As you can see, the actual path is printed without any symbolic links. It printed

instead of

os.path.dirname()

Returns the full path to the directory a file or another directory is contained in. If we split the path into a head and tail then os.path.dirname() returns the head. Here is an example…

The code snippet above should print something like…

os.path.basename()

Returns the tail of the path (as opposed to the head in case of os.path.dirname()). Here is an example…

The code snippet above should print something like…

os.chdir() and os.getcwd()

Current working directory is the directory from which a Python script is invoked and not the actual location of the script. As the names indicate, os.chdir() changes the current working directory and os.getcwd() gets the current working directory. Here is an example…

The code snippet above should print something like…

Using pathlib

pathlib is an object oriented Python standard library to handle paths and filenames instead of using plain strings as in os.path. If you do not have it installed, you can do so by running the following command…

Here is an example…

The code snippet above should print something like…

sys.argv[0]

This is a list that holds the list of command line arguments. system.argv also holds the name of the running script, exactly as it was called from the command line. Let us take an example…

The code snippet above should print something like…

os.path.abspath()

Returns a fully qualified path of the provided path which can be empty, partial or full. There are three cases…

  • Returns current working directory if the input path is empty
  • Returns a fully qualified path if the input path is partial based on the current working directory
  • If the path is already full os.path.abspath returns the provided path
  • Even if the path does not exist, it will return something, see the example

Here is an example…

abspath() vs realpath()

Notice that both abspath() and realpath() look similar so what is the difference between the two?

  • os.path.realpath() deferences symbolic links
  • os.path.abspath() removes the dots that refer to current or parent directory from the path

Here is an example…

As you can see, the os.path.abspath() function gives the fully qualified path to B without dereferencing any symboling links while os.path.realpath() gives the actual path to the file.

References

That is all for today, thanks for visiting. Feedback and questions? Use the comments section below.

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *