- 1 year ago
Get the path of the file that launched my app
Comment
1 / 1
To get the path of the file that launched your application, you can use the sys.argv
list in Python. The first element of sys.argv
is the path of the script that was executed.
Here's an example:
pythonimport sys
def get_launch_file_path():
if len(sys.argv) > 0:
return sys.argv[0]
else:
return None
launch_file_path = get_launch_file_path()
print("Launch File Path:", launch_file_path)
In this example, the get_launch_file_path()
function checks if there are any command-line arguments (sys.argv
) and returns the first argument, which is the path of the script that was executed.
You can run this code in your application to obtain the path of the file that launched it. Note that sys.argv[0]
may not always provide the absolute path, so you may need to manipulate it further if you require an absolute path.