- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to print its script name as output in Python
In this tutorial, we are going to write a program that prints the name of the Python script file. We can find the script name using the sys module.
The sys module will store all the command line arguments of python command in the sys.argv list. The first element in the list is the script name. We can extract it from that list. Python makes it easy.
Let's see the steps involved in the program.
Import the sys module.
Now, print the first element of the sys.argv list.
That's it. You got the script name.
Let's see it practically.
# importing the sys module import sys # importing os module for absolute path import os # printing the script name # first element of sys.argv list print(os.path.abspath(sys.argv))
If you run the above code, you will get the absolute path of your Python script.
If you have any doubts in the tutorial, mention them in the comment section.
- Program to print its script name as output using Python
- Python program to print the initials of a name with last name in full?
- Get output of MongoDB shell script?
- Java program to print the initials of a name with last name in full
- How to print results of script in MongoDB?
- How to obtain the same font in Matplotlib output as in LaTex output?
- Program to find an element in list whose value is same as its frequency in Python
- Python program to print Rows where all its Elements’ frequency is greater than K
- How to open a file in the same directory as a Python script?
- How to get a variable name as a string in Python?
- How to get a function name as a string in Python?
- C program to print name inside heart pattern using for loop.
- How did Google get its name?
- Python Program to Print Hello world