Hi hafsa ahmed,
You asked:
“I am taking the input directory path from the user in the GUI and i
want to pass these paths to another py file. Can you tell how can I pass
the path names from one py file to another ?”
That depends on how you are using the second py file. If you are using
it as a module, then you do this:
import second # load second.py as a module
pathname = "path/to/file" # get this from the user
second.function(pathname) # call the function with the path
That’s the way .py files are intended to be used.
The other way is to use the second .py file as a script.
# Don't do this! This is wrong and bad.
# (for experts only: this is **almost** always wrong and bad)
import os
pathname = "path/to/file" # get this from the user
commandline = 'python3 /path/to/second.py "%s"' % pathname
os.system(commandline)
This is wrong because it is slow, complicated and easy to make errors.
It is bad because unless you are really, really careful, it is
vulnerable to code injection attacks.
(If this program is only for you, and will never be shared with anyone
else, you might not care about code injection attacks. But as soon as
you allow anyone else to use it, you should care.)
Apart from os.system, there are safer, but more complicated and less
convenient, ways to call external programs and scripts but I don’t
remember what they are. The documentation for os.system should link to
those alternative ways.
This method also requires the second.py module to be written as a script
that reads input from the command line. So it must have a section that
looks something like this:
# second.py
if __name__ == '__main__':
import sys
args = sys.argv[1:]
pathname = args[0]
function(pathname)
otherwise it won’t work. There are lots of ways for a Python file to be
written to take arguments from the command line when called as a script,
so it might not look exactly like that.
Calling a Python module as a script is complicated and slow, the right
way to solve this problem is to import the module and call the function
you want directly.