About 2,980 results
Open links in new tab
  1. Get name of current script in Python - Stack Overflow

    Jul 30, 2021 · Path(__file__).name since Python3.4+. You can use __file__ to get the name of the current file. When used in the main module, this is the name of the script that was originally invoked. If you want to omit the directory part (which might be present), you can use os.path.basename(__file__).

  2. Python Program to Get the File Name From the File Path

    Aug 21, 2024 · In this article, we will be looking at the program to get the file name from the given file path in the Python programming language. Sometimes during automation, we might need the file name extracted from the file path.

  3. Extract file name from path, no matter what the os/path format

    Apr 11, 2023 · File name with extension. filepath = './dir/subdir/filename.ext' basename = os.path.basename(filepath) print(basename) # filename.ext print(type(basename)) # <class 'str'> File name without extension. basename_without_ext = os.path.splitext(os.path.basename(filepath))[0] print(basename_without_ext) # filename

  4. Get the path of the current file (script) in Python: __file__

    Aug 17, 2023 · You can use os.path.basename() and os.path.dirname() to get the file and directory name of the current script file. The result is as follows. If you want to get the name of the directory containing the current file (src in the example above), use os.path.basename(os.path.dirname(__file__)).

  5. coding style - Python file naming convention? - Software …

    Oct 27, 2017 · modules (filenames) should have short, all-lowercase names, and they can contain underscores; packages (directories) should have short, all-lowercase names, preferably without underscores; classes should use the CapWords convention.

  6. Python Get Filename From Path

    Apr 8, 2024 · There are four Python methods for getting the file name from the path: the ‘os’ module, the ‘pathlib’ module, split() and rsplit(), and regular expressions. Let’s start. Python File Name Using Os Module. The ‘os’ module in Python allows you to interact with operating systems. It has several functions that can be used to interact ...

  7. Python: Get the path and name of the file that is ... - w3resource

    Apr 16, 2025 · Write a Python program to get the absolute path of the current script. Write a function that extracts the directory name from a given file path. Write a script that gets the file name, extension, and parent directory separately.

  8. Python Read And Write File: With Examples

    Jun 26, 2022 · Learn how to open, read, and write files in Python. In addition, you'll learn how to move, copy, and delete files. With many code examples.

  9. Python Program to Get the File Name From the File Path

    basename() gives the name of the last file/folder of the path, whereas splitext() splits the file name into filename and extension. print(os.path.splitext(file_name)) Output. print(Path('/root/file.ext').stem) Output. Using stem attribute of Path module, the file name can be extracted as shown above. It works for python 3.4 and above. Also Read:

  10. File Handling in Python - GeeksforGeeks

    Jan 14, 2025 · To open a file we can use open() function, which requires file path and mode as arguments: This code opens file named geeks.txt. When opening a file, we must specify the mode we want to which specifies what we want to do with the file. Here’s a table of the different modes available: Read-only mode. Opens the file for reading.

Refresh