Searched on the search engine, no results
A detailed description of the problem: Assume that there is a "a.py" on the desktop, when a.py is run it will automatically open "b.py" (not on the desktop, but on the other path, and this path is known).
The implementation effect is similar to the following code:
Import OS Start_dire = r "E:\*.txt" OS . system (%s"%start_dire)
Reply content:
Considering that the master is a novice, I'll say a few more ways.
0. Runpy.run_path (' my_awesome_program.py ')
This method is preferred, the whole platform is universal
1. Import the file directly (if the file does not add __name__== ' __main__ '), if the filename is a variable, use __import__ (' My_awesome_program ')
2. Os.startfile (' my_awesome_program.py ')
this is windows-only., you can use the same line, a little larger items will not be used
3. Os.system (' python '%s '% ' my_awesome_program.py ')
But if the other computer's python does not hang in the%path%, it is still
for your own use only
4. Use subprocess, the command line ibid, use Baidu or check the documents yourself
The same is
for your own use only
5. You try execfile (' my_awesome_program.py '), I haven't tried, I'm not sure.
UPDATE: I tried,
can be, as if this is a platform-wide universal. 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 contents of that variable to execute another Perl script:
system($^X, "another_script.pl");
Read the. py file first, and then use the Eval function to execute the. py file content, or at least two methods with import, assuming that the b.py is in the root directory of the E-drive:
1. Add the path to the system path where the b.py is located:
importsyssys.path.append('E:/')importbb.foo()
Change Explorer.exe to Python.exe, try it.
You can also use import to execute code in other files.