- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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.
- Related Articles
- 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?
- How to print results of script in MongoDB?
- How to Insert a New Line Character in Linux Shell Script Output ?
- Python program to print Rows where all its Elements’ frequency is greater than K
- How to obtain the same font in Matplotlib output as in LaTex output?
- Python Program to Print Hello world
- Python program to print number triangle
- Python program to print an Array
- How to open a file in the same directory as a Python script?
- Python Program to Print Matrix in Z form
- Python Program to Print Numbers in an Interval
- Java program to print the initials of a name with last name in full
- How to print to console an object in a MongoDB script?