Python get current files directory

How to get full path of current file’s directory in Python?

Python’s OS module includes functions for creating and removing directories (folders), retrieving their contents, altering, and identifying the current directory, and more. To interface with the underlying operating system, you must first import the os module.

The location (path) of the executing program code can be obtained in Python. py with __file__. __file__ can be used to read other files based on the current file’s location.

Example

In the following example, the os.getcwd() function produces a string str with the absolute path to the current working directory where Python is operating

#Python program to get the path of the current working directory #Program to get the path of the file #Using getcwd() #Importing the os module import os print(' The current working directory is: ', os.getcwd()) print('File name is: ', __file__)

Output

On executing the above program, the following output is generated.

The current working directory: C:\Users\pranathi\Desktop\python prog File name: c:\users\pranathi\desktop\python prog\untitled1.py

Using os.path.basename()

In Python, the os.path.basename() method is used to get the base name of a path. To split the supplied path into a pair, this method uses the os.path.split() method internally (head, tail). After separating the supplied path into (head, tail) pairs, the os.path.basename() method returns the tail part.

Читайте также:  Java io ioexception invalid header signature

Example

In the below example, the os.path.dirname() method is used to retrieve the directory name from the supplied path.

#python program to find the basename and dirname of the path import os print('basename of the file: ', os.path.basename(__file__)) print('dirname of the file: ', os.path.dirname(__file__))

Output

On executing the above program, the following output is generated.

basename of the file: untitled1.py dirname of the file: c:\users\pranathi\desktop\python prog

Getting the absolute path of the directory

An absolute path refers to a file or folder’s location regardless of the current working directory; in actuality, it is relative to the root directory.

Example

The following example is a python program to find the absolute path.

#python program to find the absolute path import os print('absolute path of the file: ', os.path.abspath(__file__)) print('absolute path of dirname: ', os.path.dirname(os.path.abspath(__file__)))

Using os.getcwd method in python

The OS module’s getcwd() method returns a string with the absolute path to the current working directory. The trailing slash character is not included in the output string.

Example

In the following example, the os module is imported and the current working directory is obtained using the getcwd() function. The directory is printed using the print() function.

#importing the os module import os #to get the current working directory directory = os.getcwd() print(directory)

Output

On executing the above program, the following output is generated.

C:\Users\pranathi\Desktop\python prog

Example

The output will vary based on the directory you are in, but it will always begin with the root folder (for example, D:) and a directory prefixed by a.

import os absolute_path = os.path.abspath(__file__) print("Full path: " + absolute_path) print("Directory Path: " + os.path.dirname(absolute_path))

Output

On executing the above program, the following output is generated.

Full path: c:\users\pranathi\desktop\python prog\untitled1.py Directory Path: c:\users\pranathi\desktop\python prog

Источник

Get Path of the Current File in Python

Get Path of the Current File in Python

  1. Use of the pathlib Module to Get the Path of Files and Current Working Directory
  2. Use the os Module to Get the Path of Files and the Current Working Directory

In Python, we can work with many files and modules and constantly interact with the file system. To import or export files, we need to know the correct path and directory of such files; otherwise, errors are raised. It is also essential to know the path of the currently running Python script or the path of some other file.

The current working directory is the directory of Python in which we are currently working on.

This tutorial will introduce how to get the full path and directories of the current file and the current working directory in Python.

Use of the pathlib Module to Get the Path of Files and Current Working Directory

The pathlib library is available to Python 3.x and up and contains classes, methods to handle file-path-related problems.

In the code below, we will extract the path of a Python script using the functions provided by the pathlib module:

import pathlib  print(pathlib.Path(__file__).parent.absolute()) 

The absolute() method returns the full path of the file, and the parent() function retrieves the directory of the file from this path.

To get the current working directory, we remove the file name from the above function. The following code shows how:

import pathlib print(pathlib.Path().absolute()) 

Use the os Module to Get the Path of Files and the Current Working Directory

The os library is used to interact with the Operating System and has functions available to retrieve full paths of the files. This method works with Python 2.x as well.

The abspath() function can get the required file’s path, and the dirname() function gets the directory from the full path.

import os  print(os.path.dirname(os.path.abspath(__file__))) 

We cannot use the dirname() function directly to get a file’s directory as it returns an empty string.

We can also use the realpath() function as an alternative to the abspath() function. It removes any symbolic links if present from the path.

import os  print(os.path.dirname(os.path.realpath(__file__))) 

To get the current working directory, we can use the getcwd() method that returns the current directory path. We can pass this path to the dirname() method to get the directory.

import os print(os.path.abspath(os.getcwd())) 

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

Related Article — Python Path

Источник

Python Get Current Directory – Print Working Directory PWD Equivalent

Dionysia Lemonaki

Dionysia Lemonaki

Python Get Current Directory – Print Working Directory PWD Equivalent

In this article, you will learn how to get the current working directory (another name for folder) in Python, which is the equivalent of using the pwd command.

There are a couple of ways to get the current working directory in Python:

  • By using the os module and the os.getcwd() method.
  • By using the pathlib module and the Path.cwd() method.

How to Get The Current Directory Using the os.getcwd() Method in Python

The os module, which is part of the standard Python library (also known as stdlib), allows you to access and interact with your operating system.

To use the os module in your project, you need to include the following line at the top of your Python file:

Once you have imported the os module, you have access to the os.getcwd() method, which allows you to get the full path of the current working directory.

Let’s look at the following example:

import os # get the current working directory current_working_directory = os.getcwd() # print output to the console print(current_working_directory) # output will look something similar to this on a macOS system # /Users/dionysialemonaki/Documents/my-projects/python-project 

The output is a string that contains the absolute path to the current working directory – in this case, python-project .

To check the data type of the output, use the type() function like so:

print(type(current_working_directory)) # output #

Note that the current working directory doesn’t have a trailing forward slash, / .

Keep in mind also that output will vary depending on the directory you are running the Python script from as well as your Operating System.

How to Get The Current Directory Using the Path.cwd() Method in Python

In the previous section, you saw how to use the os module to get the current working directory. However, you can use the pathlib module to achieve the same result.

The pathlib module was introduced in the standard library in Python’s 3.4 version and offers an object-oriented way to work with filesystem paths and handle files.

To use the pathlib module, you first need to import it at the top of your Python file:

Once you have imported the pathlib module, you can use the Path.cwd() class method, which allows you to get the current working directory.

Let’s look at the following example:

from pathlib import Path # get the current working directory current_working_directory = Path.cwd() # print output to the console print(current_working_directory) # output will look something similar to this on a macOS system # /Users/dionysialemonaki/Documents/my-projects/python-project 

As you can see, the output is the same as the output I got when I used the os.getcwd() method. The only difference is the data type of the output:

print(type(current_working_directory)) # output #

Conclusion

And there you have it! You now know how to get the full path to the current directory in Python using both the os and pathlib modules.

Thanks for reading, and happy coding!

Источник

Оцените статью