A detailed description of the problem that has not been found on the search engine: Suppose there is A ". py ", when. when py is run, "B. "py" (not on the desktop, but on another path, and the path is known ). The implementation result is similar to the following code: importosstart_direrE: * .txtos.system(assumer.exe % s % start_dire) searched on the search engine.
A detailed description of the problem: Suppose there is A ". py ", when. when py is run, "B. "py" (not on the desktop, but on another path, and the path is known ).
The implementation result is similar to the following code:
import osstart_dire = r"E:\*.txt"os.system("explorer.exe %s" %start_dire)
Reply content: considering that the subject is a newbie, I will discuss a few more methods.
0. runpy. run_path ('My _ awesome_program.py ')
This method is preferred and is common across the Platform
1. directly import the file (If NO _ name __= = '_ main _' is added to the file). If the file name is a variable, use _ import _ ('My _ awesome_program ')
2. OS. startfile ('My _ awesome_program.py ')
This is windows-onlyIt's okay to use it on your own. A project that is a little bigger will not be used.
3. OS. system ('python "% s" '% 'my _ awesome_program.py ')
However, if the peer computer's python is not in % path %
For your own use only
4. Use subprocess. The command line is the same as above. use Baidu or query the document on your own.
Also
For your own use only
5. Try execfile ('My _ awesome_program.py ').
UPDATE: I have tried,
YesIt seems that this is also universal across the platform. Python does not know, but the Perl language has a built-in variable $ ^ X whose content is the path of the Perl interpreter that is executing the current script. You can use the variable content to execute another Perl script:
system($^X, "another_script.pl");
Read the. py file first, then use the eval function to execute the. py file content, or use the import function in at least two ways. Assume that B. py is in the root directory of the E Disk:
1. Add the path of B. py to the system path:
import syssys.path.append('E:/')import bb.foo()
Replace assumer.exewith python.exe.
You can also run the code in other files through import.