简体   繁体   English

PYTHON 和批处理脚本:如果文件存在则运行文件,如果不存在则创建

[英]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 文件的批处理文件(因此,如果我更换计算机,我可以轻松创建此脚本),它将执行以下操作

  1. Check if volume_keys.ahk exists in the D Drive检查D盘中是否存在volume_keys.ahk
  2. If it exists, run that;如果存在,运行它;
  3. If it doesn't exist, then create a file named volume_keys.ahk and add my script to it.如果它不存在,则创建一个名为 volume_keys.ahk 的文件并将我的脚本添加到其中。

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.

相关问题 当我尝试在批处理文件中使用python脚本写入文件然后运行它时。 它不会写入文件 - When I try to write to a file using python script in a batch file and then run it. It doesn't write to the file Python 脚本不创建文件 - Python Script doesn't create file 批处理文件中的Python脚本无法在任务计划程序中运行 - Python script from batch file won't run in task scheduler 如何让 Python 检查文件是否存在,如果不存在则创建它? - How to have Python check if a file exists and create it if it doesn't? 当我打开一个批处理文件时,它应该使用 python 创建另一个文件,python 打开它,但它不会创建文件 - When I open a batch File that is supossed to create another file with python, python opens it but it doesn't create the file 作为systemd服务运行时,Python脚本不会写入文件 - Python script doesn't write to file when run as a systemd service Scheduler使python脚本运行但不生成csv文件 - Scheduler makes a python script run but doesn't produce a csv file 无法从批处理文件运行python脚本 - unable to run python script from Batch file 批处理文件解压文件并运行 Python 脚本 - Batch file to unzip files and run Python script 删除文件(如果存在)。如果没有,请创建它 - Delete a file if it exists. If it doesn't, create it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM