[英]PYTHON AND BATCH SCRIPT: Run file if it exists and create if it doesn't
Full Disclaimer: I DO NOT KNOW PYTHON.完全免责声明:我不知道 PYTHON。
Hi Guys,嗨,大家好,
I have made an AutoHotKey Script for my volume keys.我为我的音量键制作了一个 AutoHotKey 脚本。 I would like to create a batch file which runs a python file (so if I change computers, I can easily create this scripts) which would do the following我想创建一个运行 python 文件的批处理文件(因此,如果我更换计算机,我可以轻松创建此脚本),它将执行以下操作
My script is:我的脚本是:
^!NumpadMult::Send {Volume_Mute}
^!NumpadAdd::Send {Volume_Up}
^!NumpadSub::Send {Volume_Down}
I know how to code the.bat file and just need help for the python point-of-view, but I request the community to check it:我知道如何编写 .bat 文件,只需要 python 观点的帮助,但我要求社区检查一下:
@ECHO OFF
ECHO This script will run an AHK Script. If you want to stop this process from happening, then cross this window off.If you want to continye:
pause
cd d:
D:\run_volume_keys_ahk_script.py
I really appreciate any help by the community.我非常感谢社区的任何帮助。
Thanks in advance提前致谢
You can use the os
library for this.您可以为此使用os
库。 Here's what the python program would look like.这是 python 程序的样子。
import os
if os.path.isfile('D:\\volume_keys.ahk'): # check if it exists
os.system('D:\\volume_keys.ahk') # execute it
else:
with open('D:\\volume_keys.ahk', 'w') as f: # open it in w (write) mode
f.write('^!NumpadMult::Send {Volume_Mute} \
^!NumpadAdd::Send {Volume_Up} \
^!NumpadSub::Send {Volume_Down}') # Write to file
os.system('D:\\volume_keys.ahk') # execute
To activate the ahk script, you might want to use the subprocess
module, of which I took the example from here要激活 ahk 脚本,您可能需要使用subprocess
模块,我从这里举了一个例子
import subprocess
subprocess.call(["path/to/ahk.exe", "script.ahk"])
Note that you'll have to find the ahk executable on a computer before you can use the script, maybe you want to automatically check that too.请注意,您必须先在计算机上找到 ahk 可执行文件,然后才能使用该脚本,也许您也想自动检查它。
You can set the path you want to check for scripts in one string, and then add the filenames of your scripts as strings to a list.您可以在一个字符串中设置要检查脚本的路径,然后将脚本的文件名作为字符串添加到列表中。 You can use listdir()
from the os
module to see any files and directories at a given path, then iterate over your scriptnames and check if it exists in that list of files.您可以使用os
模块中的listdir()
来查看给定路径中的任何文件和目录,然后遍历您的脚本名并检查它是否存在于该文件列表中。 If it does, run it.如果是,请运行它。
In this example I copy-pasted your script into a string as value for the key 'scriptname' in a dictionary, so that python can actually create the script file.在此示例中,我将您的脚本复制粘贴到一个字符串中,作为字典中键“scriptname”的值,以便 python 实际上可以创建脚本文件。 This isn't really a neat way to do it though, you might want to have your scripts prepared in a directory next to your python script and copy them from there.不过,这并不是真正的好方法,您可能希望在 python 脚本旁边的目录中准备好脚本并从那里复制它们。 See an example of how here请参阅此处的示例
from os import listdir
from os.path import isfile, join
CHECK_PATH = "D:"
AHK_EXECUTABLE_PATH = "path/to/ahk.exe"
SCRIPTS_TO_CHECK = {'script1.ahk':"""^!NumpadMult::Send {Volume_Mute}
^!NumpadAdd::Send {Volume_Up}
^!NumpadSub::Send {Volume_Down} """, 'script2.ahk':" some other script here"}
files_to_check = set(listdir(CHECK_PATH)) # using a set for fast lookup later
for scriptname, script in SCRIPTS_TO_CHECK.items():
if not scriptname in files_to_check:
print(f"script {scriptname} not found, creating it.")
with open(scriptname, 'w') as file:
file.write(script)
# else
subprocess.call(AHK_EXECUTABLE_PATH, scriptname)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.