Jun-23-2023, 04:43 AM
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:
calltestren.py:
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< >