python - Exec function working for one method, and not the other -


context: part of code running inside of class called commands, within method called looper. idea code, when input python command line, if 1 word, runs method name. if greater 1 word, take takes first word, make method command, , else argument.

cmd =  input("user>>> ")  cmdsplit = cmd.split() lencmd = len(cmdsplit)  if (lencmd== 1):     cmd = "commands." + cmd + "()"     print(type(cmd))     logging.debug("this length of 1")     try:         exec (cmd)     except:         print ("not valid command.") 

it appears, when type chrome (one of methods posted below) line, exec breaks, , wont work. if use hearth (the other method, works fine). cant see reason chrome not work, hearth work. exec not word chrome?

def chrome():     subprocess.popen("chrome.bat", cwd=r"c:\cmdcommands")  def hearth():     subprocess.popen("hearth.bat", cwd=r"c:\cmdcommands") 

output program:

user>>> chrome <class 'str'> debug:root:this length of 1 not valid command user>>> hearth <class 'str'> debug:root:this length of 1 user>>>     

you used hanging try: , except: statement, mentioned in comments.

instead, specific exceptions can used more accurately pinpoint cause of issues in python program, such valueerror or typeerror.


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -