π‘ Problem Formulation: In Python scripting, you might want to display or use the script’s filename programmatically. For instance, if your script is named example.py, you want to write a program that, when run, will output “example.py” as its result. This can be useful for logging, debugging, or when building command-line tools that provide feedback to the user.
Method 1: Using sys.argv[0]
In Python, the sys module provides access to some variables used or maintained by the Python interpreter. sys.argv is a list in Python, which contains the command-line arguments passed to the script. The first item in this list, sys.argv[0], is always the script name.
Here’s an example:
import sys
print("Script name:", sys.argv[0])Output:
Script name: example.py
This code snippet imports the sys module and prints the first element of sys.argv, which is the name of the script. This method is straightforward and widely used for accessing the script name.
Method 2: Using __file__
The __file__ attribute is a special attribute of Python modules that contains the path to the module’s source file. When used within a Python script, __file__ provides the script’s filename along with its full path.
Here’s an example:
print("Script name:", __file__)Output:
Script name: /path/to/your/script/example.py
By printing the __file__ attribute, you’ll get the path and the name of the currently executing script. You might need additional processing if you want just the name without the path.
Method 3: Using os.path.basename() with __file__
To extract just the script name and not the full path, the os.path module provides the function basename(), which can be used in conjunction with the __file__ attribute to get only the script’s file name.
Here’s an example:
import os
print("Script name:", os.path.basename(__file__))Output:
Script name: example.py
This snippet first imports the os module then uses os.path.basename() to get the base name of the script. This approach is useful for scripts where you need just the filename, without the directory.
Method 4: Using pathlib.Path()
The pathlib module, introduced in Python 3.4, provides an object-oriented approach to filesystem paths. Using Path() with the __file__ attribute, you can easily extract the scriptβs name.
Here’s an example:
from pathlib import Path
print("Script name:", Path(__file__).name)Output:
Script name: example.py
This code uses the Path class from the pathlib module to wrap the __file__ attribute. The .name property then provides just the script name, not the full path.
Bonus One-Liner Method 5: Lambda Function
For a minimalistic one-liner approach, you can use a lambda function to extract the script name. This method combines Python’s functional programming features with the versatility of other techniques mentioned above.
Here’s an example:
script_name = lambda: os.path.basename(__file__)
print("Script name:", script_name())Output:
Script name: example.py
This simple one-liner defines a lambda function that, when called, returns the basename of the file containing the script. It’s a compact alternative to defining a full function.
Summary/Discussion
- Method 1: sys.argv[0]. Straightforward and standard. Might not work properly if the script is executed differently (e.g., from an IDE or a different entry point).
- Method 2: __file__. Simple but includes the full path. Requires extra steps to extract just the filename.
- Method 3: os.path.basename()with__file__. Clean and direct. Gives just the script name. Requires importing an additional module.
- Method 4: pathlib.Path(). Modern and object-oriented. Can handle paths across different Operating Systems consistently.
- Bonus Method 5: Lambda Function. Compact and functional. Combines ease of os.path.basename()with the brevity of a lambda.
 
 