Python Forum
calling external function with arguments
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calling external function with arguments
#1
I am new to python...

I am trying to call a function defined in a separate program, but I cannot figure out how to pass an argument. How to do?

The function I want to call is in this program:
testren.py:
import sys, getopt
global old_name
def renameit(argv):
   global old_name
   old_name = ''
   opts, args = getopt.getopt(argv,"ho:",["oldname="])
   for opt, arg in opts:
      if opt in ("-o", "--old_name"):
         old_name = arg  
   print ('in testren after - old_name =', old_name )

if __name__ == "__main__":
   print ('sys.argv ', sys.argv[1:])
   renameit(sys.argv[1:])
If I call testren.py directly, I get the desired results:
Output:
C:\python>python testren.py -o "my1.mp3" sys.argv ['-o', 'my1.mp3'] in testren after - old_name = my1.mp3
If I call it from another python program the arguments are not found, I cannot figure out how to pass the arguments. How to do?

calltestren.py:
import testren
if __name__ == '__main__':
    renargs = '-o "my1.mp3"'
    print('calling rename', renargs)
    testren.renameit(renargs)
    print('old_name<', old_name, '>')
I just get blanks where I expect a file name to be:
Output:
C:\python>python calltestren.py calling rename -o "my1.mp3" in testren after - old_name = old_name< >
Reply


Messages In This Thread
calling external function with arguments - by Wimpy_Wellington - Jun-23-2023, 04:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 986 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Calling a function (which accesses a library) from another file mouse9095 4 850 Jun-07-2023, 08:55 PM
Last Post: deanhystad
Sad Iterate randint() multiple times when calling a function Jake123 2 2,109 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Calling a class from a function jc4d 5 1,870 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  'namespace' shorthand for function arguments? shadowphile 5 2,654 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  Checking the number of arguments a function takes Chirumer 3 2,211 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  [Solved] TypeError when calling function Laplace12 2 2,934 Jun-16-2021, 02:46 PM
Last Post: Laplace12
  Possible to dynamically pass arguments to a function? grimm1111 2 2,229 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  calling a function and argument in an input phillup7 3 2,646 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  How to pass multiple arguments into function Mekala 4 2,486 Jul-11-2020, 07:03 AM
Last Post: Mekala

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020