All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Get Directory Of File Python

Last Updated : Mar 11, 2024

Get Directory Of File Python

In this article we will show you the solution of get directory of file python, a file is an important part of the Python programming language when it comes to storing or preserving data.

For writing or reading to a file, or performing various operations on it, we must know its correct path; otherwise, an error will occur.

The relative path is used when making changes to a file in the same directory while you are working in another directory.

A file in another directory needs to be changed with the absolute path if we are going to make changes.

In Python, we have a number of functions available which allow us to get the path of the running .py file (the one we are currently working on).

The functions listed below are path functions. These functions will conserve the defaults, and also be used by os.getcwd(), pathlib.Path().absolute(), and os.path.basename.

Following this section, we will discuss every function in more detail.

As we have discussed relative and absolute paths above, let me briefly explain the differences between these two terms.

The absolute path specifies the file's relative location or contains the complete file or directory location, whereas the relative path describes the file's location with respect to the current working directory.

We will now discuss the idea of how to get directory of file python with example.

Step By Step Guide On Get Directory Of File Python :-

import os
print(os.getcwd())
C:\Python33
# list all sub-directories
os.listdir()
['DLLs',
'Doc',
'include',
'Lib',
'libs',
'LICENSE.txt',
'NEWS.txt',
'python.exe',
'pythonw.exe',
'README.txt',
'Scripts',
'tcl',
'Tools']
os.listdir('G:\\')
['$RECYCLE.BIN',
'Movies',
'Music',
'System Volume Information']
  1. A module that provides access to the underlying operating system is imported by the "os" import statement.
  2. As the first line of code shows, it calls the "getcwd()" function in the "os" module, which returns the current directory.
  3. As a result, "C:/Python33" is printed on the console; that is, the print statement prints the result to the console.
  4. As shown in the second set of code below, the "listdir()" function from the "os" module is called without any arguments to list all files and directories in the current working directory.
  5. Following the print statement, the Python33 directory is displayed on the console, which shows all sub-directories.
  6. Lastly, the third set of code uses the "listdir()" function from the "os" module in order to list a specific directory, in this case G:/.
  7. Printing this result to the console is then done with the print statement.

Conclusion :-

As a result, we have successfully learned how to get directory of file python.

Python provides you with a number of methods for interacting with files and directories, such as those we discussed today.

You can create, rename, and delete a Python directory using these commands.

In addition, you can traverse them and determine whether a path exists.

I hope this article on get directory of file python helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪