When you are playing with Python, you may occasionally want to run python automatically. Therefore, I would like to introduce how to execute the python file automatically after making a memorandum. The main method is to create and execute ** .bat file ** on Windows and ** create and execute ShellScript ** on Mac. Neither requires the installation of special software, so you can feel free to do it. If anyone knows a better way than these, it would be greatly appreciated if you could comment.
For Windows, create a .bat file. If you want to automatically execute the python files of test_1.py
and test_2.py
, create a bat file as follows. To create a bat file, simply write the following code using a suitable text editor, and finally change the extension to .bat.
hoge.bat
python test_1.py
python test_2.py
When the bat file is created, click hoge.bat
and the command prompt will be automatically launched and executed. However, please note that if the execution is a command prompt, it will close without permission when the process is completed. It is recommended to make the log spit out.
If you're a regular PowerShell user, you can just drag it into PowerShell and press Enter.
The Mac does the same thing as creating a bat file in Windows. However, on Mac, it is not a bat file but an sh file.
Suppose you want to autorun the test_1.py
and test_2.py
python files as you did for windows. For Mac, create the following sh file. To create an sh file, just write the following code using a suitable text editor and change the extension to .sh.
hoge.sh
python test_1.py
python test_2.py
For Mac, run the sh file from the terminal. You can run it by opening a terminal, going to the directory containing the sh file you just created and tapping sh hoge.sh
. You can also run the sh file with ./hoge.sh
by granting permissions with chmod w + x hoge.sh
in the same directory.
The last is a very unsmart way. However, I think it is strong (?) For complicated GUI operations + execution of python files. To do this, use software called UWSC. The free version is fine. If you download uwsc and start it, it will look like the one below.
This software is a convenient software that records GUI operations and executes them as they are. Press the second record button from the right or press the ALT key + F3 to start recording. If you want to play the recorded operation, press the ALT key + F1 to execute the recorded process. Also, since the stored operations can be saved as a uws file, it is convenient when you want to perform complicated GUI operations + python file execution.
Basic usage of batch file Shell Script Reference UWSC Download and Usage
Recommended Posts