

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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.
Example
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[0]))
Output
If you run the above code, you will get the absolute path of your Python script.
C:\Users\hafeezulkareem\Desktop\sample\tutorialspoint.py
Conclusion
If you have any doubts in the tutorial, mention them in the comment section.
- Related Questions & Answers
- 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 execute a JavaScript function when I have its name as a string?
- 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
Advertisements