Change language

Get the directory of the current Python script

| |

The concept of current working directory (CWD) becomes important here. Consider CWD as a folder inside which Python runs. Whenever files are called by their name alone, Python assumes that it runs in the CWD, which means that the name-only reference will only succeed if the file is in the Python CWD.

Note . The folder where the Python script is executed is called Current Directory. This is not the path where the Python script is located.

Getting the current working directory

Python provides OS module for interacting with the operating system. This module is included in the standard Python utility. All functions in the os module call OSError for invalid or unavailable file names and paths or other arguments that are of the correct type but are not accepted by the operating system.

# Python program to explain the os.getcwd () method

  
# import of the os module

import os 

 
# Get current work
# directory (CWD)

cwd = os.getcwd () 

  
# Print current job
# directory (CWD)

print ( "Current working directory:" )

print (cwd) 

Exit:

Note. To learn more about os.getcwd () click here

Get script path

realpath-method rel="noopener noreferrer" target="_blank"> os.path.realpath () can be used to get the path to the current Pyhton script. Actually realpath-method/ rel=noopener target=_blank> os.path.realpath () in Python is used to get the canonical path of a specified filename by eliminating any symbolic links encountered in the path. The special variable __file__ is passed to realpath () to get the path to the Python script.

Note: __file__ — this is the path to the file from which the module was loaded, if it was loaded from a file.

Syntax: os.path.realpath (path)

Parameter:
path: A path-like object representing the file system path.
A path-like object is either a string or bytes object representing a path.

Return Type: This method returns a string value which represents the canonical path.

Exmaple:

# Python program to get
# script path

 

 

import os

 
# Get current job # catalog (CWD)

cwd = os.getcwd () 

print ( "Current Directory:" , cwd)

 
# Get directory
# script

script = os.path.realpath (__ file__)

print ( "SCript path:" , script)

Output:

Note. To learn more about os.path.realpath () realpath-method rel="noopener noreferrer" target="_blank"> click here .

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically