为什么我的代码不能在命令行中工作?

2024-09-27 09:34:52 发布

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

我正在使用Python3.6,需要在命令行中运行代码。当我在PyCharm中运行代码时,代码可以工作,但当我使用命令行时,会出现以下错误:

 File "path", line 43, in <module>
     rb = ds.GetRasterBand(1) 
AttributeError: 'NoneType' object has no attribute 'GetRasterBand'

我好像对这些台词有点意见:

ds = gdal.Open('tif_file.tif', gdal.GA_ReadOnly)
rb = ds.GetRasterBand(1)
img_array = rb.ReadAsArray()

有人知道我可能做错了什么吗?你知道吗

编辑

一些魔法发生了。今天早上我试着运行我的代码,一切都很好。我想我的电脑需要的是重启什么的。谢谢大家的帮助。你知道吗


Tags: path代码命令行in命令错误lineds
1条回答
网友
1楼 · 发布于 2024-09-27 09:34:52

from the gdal documentation:

from osgeo import gdal
dataset = gdal.Open(filename, gdal.GA_ReadOnly)
if not dataset:
    ...

Note that if GDALOpen() returns NULL it means the open failed, and that an error messages will already have been emitted via CPLError(). If you want to control how errors are reported to the user review the CPLError() documentation. Generally speaking all of GDAL uses CPLError() for error reporting. Also, note that pszFilename need not actually be the name of a physical file (though it usually is). It's interpretation is driver dependent, and it might be an URL, a filename with additional parameters added at the end controlling the open or almost anything. Please try not to limit GDAL file selection dialogs to only selecting physical files.


看起来您试图打开的文件不是有效的gdal文件,或者文件选择中正在进行其他操作。您可以尝试将程序定向到一个已知良好的在线文件来测试它。你知道吗

相关问题 更多 >

    热门问题