批处理文件错误地设置了Python脚本的根目录

2024-04-24 03:22:12 发布

您现在位置:Python中文网/ 问答频道 /正文

问题

我有一个gdal2tiles.bat文件,上面写着

@echo off
call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
python "%OSGEO4W_ROOT%\apps\Python39\Scripts\gdal2tiles.py" %*

但是,当我以gdal2tiles earth.tifgdal2tiles.bat earth.tif的形式从Powershell运行脚本时,错误消息显示

The system cannot find the path specified. python: can't open file 'C:\apps\Python39\Scripts\gdal2tiles.py': [Errno 2] No such file or directory

解决方法

我编辑.bat文件以放置.py文件的文件路径,如下面的代码所示。我运行它,它说The system cannot find the path specified.,然后脚本按预期运行,输出正确

@echo off
call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
python "C:\OSGeo4W\apps\Python39\Scripts\gdal2tiles.py" %*

然而,gdal2tiles.py只是该文件夹中众多Python脚本中的一个。如果我需要运行另一个脚本,我需要再次编辑它的.bat文件

Batch files in folder

我以前的帖子与此类似https://gis.stackexchange.com/q/414053/146811

不需要我编辑所有.bat文件的好方法是什么?