Python текущая папка скрипта

How to get current directory in Python?

In this article, we will take a look at how to get current directory in Python. The current directory is nothing but your working directory from which your script is getting executed.

Getting the Current Working Directory in Python

The os module has a getcwd() function using which we can find out the absolute path of the working directory.

Syntax: os.getcwd()

Parameter: None

Return Value: Returns the string which contains the absolute path of the current working directory.

Below is an example to print the current working directory in Python

# Python program get current working directory using os.getcwd() # importing os module import os # Get the current directory path current_directory = os.getcwd() # Print the current working directory print("Current working directory:", current_directory) 
Current working directory: C:\Projects\Tryouts

Get the path of the script file in Python

If you want to get the path of the current script file, you could use a variable __file__ and pass it to the realpath method of the os.path module.

Example to get the path of the current script file

# Python program get current working directory using os.getcwd() # importing os module import os # Get the current directory path current_directory = os.getcwd() # Print the current working directory print("Current working directory:", current_directory) # Get the script path and the file name foldername = os.path.basename(current_directory) scriptpath = os.path.realpath(__file__) # Print the script file absolute path print("Script file path is : " + scriptpath)
Current working directory: C:\Projects\Tryouts Script path is : C:\Projects\Tryouts\main.py

Changing the Current Working Directory in Python

If you want to change the current working directory in Python, use the chrdir() method.

Syntax: os.chdir(path)

Читайте также:  Php вывод командной строки

path: The path of the new directory in the string format.

Returns: Doesn’t return any value

Example to change the current working directory in Python

# Import the os module import os # Print the current working directory print("Current working directory: ".format(os.getcwd())) # Change the current working directory os.chdir('/Projects') # Print the current working directory print("New Current working directory: ".format(os.getcwd())) 
Current working directory: C:\Projects\Tryouts New Current working directory: C:\Projects

Conclusion

To get the current working directory in Python, use the os module function os.getcwd() , and if you want to change the current directory, use the os.chrdir() method.

Источник

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.

Читайте также:  Javascript date utc to local

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.

Читайте также:  Спецсимволы для html стрелки

Thanks for reading, and happy coding!

Источник

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