How do I call function inside another function in python?

Viewed 64

I have a small python app that runs python and bash commands. There are 2 files, main.py that has python code and second file bash_file.py that has bash commands!

The main.py run one function that It is located in second file(bash_file). I want when function is success then call the second function.

How can I do that?

I am sorry! I am not native speaker so maybe I have some writing errors.

main.py

#!/usr/bin/python3
import os
import subprocess
import bash_file
  
    
def button_block(self):
    bash_file.connection()

SECOND FILE bash_file.py

import subprocess
    
def connection():
    subprocess.run(["pkexec", '/bin/bash', '-c', country], check=True)
    
country = '''
    
aa=$(wget -qO- http://ipecho.net/plain | xargs echo)
geoiplookup $aa
    
if geoiplookup $aa | grep us ; then
    echo "It's us"
############### HERE I WANT TO CALL SECOND FUNCTION  second_function() ###########
else
    echo "It isn't us"
    exit 0
fi
'''


def second_function():
    subprocess.run(["pkexec", '/bin/bash', '-c', test], check=True)
    
test = '''

echo "this is second function"
    
'''
1 Answers

Since you're using check=True, if the call fails, it will raise an exception, and the program will stop. And if it's successfull, it will continue running.

So just add second function invocation to button_block. That is, append bash_file.second_function().

So the function in your first file will look like this:

def button_block(self):
    bash_file.connection()
    bash_file.second_function()
Related