我试图在我的谷歌Colab笔记本上运行。bat文件,但是我似乎不能让它发生。当我导航到文件夹时,代码显示该目录或文件不存在。
from subprocess import Popen
p = Popen("batch.bat", cwd=r"/content/drive/MyDrive/sd/stable-diffusion/merge-models-main/")
stdout, stderr = p.communicate()
Colab是一个Ubuntu Linux环境,所以如果要运行的文件包含类似Windows的命令,它将会挣扎。如果文件包含Linux shell命令,那么下面的代码将演示如何执行这些命令。
这个单元格生成一个batch.bat
文件(纯粹主义者会认为它应该是batch.sh
)。
# This is a Unix shell script
with open('batch.bat', 'w') as f:
f.write('var=$(date)rn')
f.write('echo "$var" > output.txtrn')
默认放置在/content/
目录下。如果你想使用你自己的Google Drive上的文件,你必须自己挂载。
要执行文件中的命令,请这样做。注意Popen
是如何将包含要执行的文件位置的列表作为第二个参数的。
from subprocess import Popen
p = Popen(["/bin/sh", "/content/batch.bat"])
stdout, stderr = p.communicate()
查找文件output.txt
并观察其中的时间戳。这应该提示它是否在工作。