Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

How to get current directory and file path in Python

To get the current directory and the file path of the currently executing script in Python, you can use the os and os.path modules from the Python standard library.

Here's an example:

import os

# Get the current directory
current_directory = os.getcwd()

# Print the current directory
print("The current directory is:", current_directory)

In this example, we import the os module and use the os.getcwd() function to get the current directory as a string. Then, we print the current directory.

To get the file path of the currently executing script, you can use the __file__ attribute in combination with the os.path module:

import os

# Get the absolute path of the currently executing script
script_path = os.path.abspath(__file__)

# Print the script path
print("The script path is:", script_path)

In this example, we use the __file__ attribute, which returns the relative path of the currently executing script, and the os.path.abspath() function to convert the relative path to an absolute path. Then, we print the script path.

Please note that the __file__ attribute may not work as expected when running the script interactively, such as in a Python shell or a Jupyter Notebook. In such cases, you can use the os.path.realpath() function with the __file__ attribute to get the correct file path:

import os

# Get the absolute path of the currently executing script
script_path = os.path.realpath(__file__)

# Print the script path
print("The script path is:", script_path)

In this example, we use the os.path.realpath() function with the __file__ attribute to get the absolute path of the currently executing script.

  1. Get current directory in Python:

    import os
    
    current_directory = os.getcwd()
    print(f"Current directory: {current_directory}")
    
  2. Get current file path in Python:

    import os
    
    current_file_path = os.path.abspath(__file__)
    print(f"Current file path: {current_file_path}")
    
  3. Current file path using file in Python:

    current_file_path = os.path.realpath(__file__)
    print(f"Current file path using file: {current_file_path}")
    
  4. Using pathlib.Path() for current file path in Python:

    from pathlib import Path
    
    current_file_path = Path(__file__).resolve()
    print(f"Current file path using pathlib: {current_file_path}")
    
  5. Get current directory and file path separately in Python:

    import os
    
    current_directory = os.path.dirname(os.path.abspath(__file__))
    current_file_name = os.path.basename(__file__)
    
    print(f"Current directory: {current_directory}")
    print(f"Current file name: {current_file_name}")
    
  6. Current file path with os.path.join() in Python:

    import os
    
    current_file_path = os.path.join(os.getcwd(), __file__)
    print(f"Current file path using os.path.join(): {current_file_path}")
    
  7. Get current directory and file name without extension in Python:

    import os
    
    current_directory = os.path.dirname(os.path.abspath(__file__))
    current_file_name = os.path.splitext(os.path.basename(__file__))[0]
    
    print(f"Current directory: {current_directory}")
    print(f"Current file name without extension: {current_file_name}")
    
  8. Finding parent directory in Python script:

    import os
    
    parent_directory = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
    print(f"Parent directory: {parent_directory}")
    
  9. Get current file path with sys.argv[0] in Python:

    import sys
    
    current_file_path = os.path.abspath(sys.argv[0])
    print(f"Current file path using sys.argv[0]: {current_file_path}")
    
  10. Python pathlib.Path.cwd() for current working directory:

    from pathlib import Path
    
    current_working_directory = Path.cwd()
    print(f"Current working directory using pathlib: {current_working_directory}")