简体   繁体   中英

python3 : Identify if a python script is executed by bash or powershell

I have a python script that I run by power shell and git-bash . Is there any way to identify in python code if the script is executed in power shell or git-bash

You can examine the process tree that the current process belongs to.

import psutil
import os
this_proc = psutil.Process(os.getpid()) # Get process information for this process
parent_proc = psutil.Process(os.getppid()) # Get process information for the parent process
print("Parent: {}".format(parent_proc.name()))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM