Lets say you have the following code
def set_args():
#Set Arguments
parser = argparse.ArgumentParser()
parser.add_argument("-test", help = 'test')
return parser
def run_code():
def fileCommand():
print("the file type is\n")
os.system("file " + filename).read().strip()
def main():
parser = set_args()
args = parser.parse_args()
what is best way to call that fileCommand() function from def main():?
I have the following code which of course doesn't work:
def main():
parser = set_args()
args = parser.parse_args()
#If -test,
if args.test:
filename=args.test
fileCommand()
So if you were to run python test.py -test test.txt it should run the file command on it to get the file type
I know if I keep messing around I'll get it one way or another, but I typically start to over complicated things so its harder down the line later. So what is the proper way to call nested functions?
Thanks in advance!
run_code, and goes away oncerun_codereturns and the variable goes out of scope. (Never mind the fact that you never callrun_code, meaning the function is never defined at all.) Why are you defining it insiderun_codein the first place?fileCommand()after its definition, inrun_code()'s body. Or even not to nest it if you don't need to.os.system()doesn't return the output of the command; it returnsNone. Just drop thereadandstripfunctions, asfilewill just write to the standard output it inherits from your script.s = os.popenfor the file command that worked, but some of the commands I have require os.system so I switched it to get a broader answer to incorporate the os.system without even running it first.