3

我正在尝试编写一个 Python 代码,通过调整以下代码将 LANDSAT 卫星图像导入 Grass GIS:http: //grass.osgeo.org/wiki/LANDSAT

LANDSAT 切片作为文件夹下载,每个文件夹包含 7 个 tiff 图像(波段 1-7)。因此,我有一个目录,其中包含多个子目录(每个 LANDSAT 瓦片一个)。

我目前的代码如下:

#!/usr/bin/python

import os
import sys
import glob
import grass.script as grass

def import_tifs(dirpath):

    for dirpath, dirname, filenames in os.walk(dirpath):
        for dirname in dirpath:

        dirname = os.path.join(dirpath,dirname)

            for file in os.listdir(dirname):
                if os.path.splitext(file)[-1] != '.TIF':
                    continue
                ffile = os.path.join(dirname, file)
                name = os.path.splitext(file)[0].split(dirname)[-1]

                grass.message('Importing %s -> %s@%s...' % (file, name, dirpath))

                grass.run_command('r.in.gdal',
                                  flags = 'o',
                                  input = ffile,
                                  output = name,
                                  quiet = True,
                                  overwrite = True)

def main():                                 
    if len(sys.argv) == 1:
        for directory in filter(os.path.isdir, os.listdir(os.getcwd())):
            import_tifs(directory)
    else:
        import_tifs(sys.argv[1])

if __name__ == "__main__":
    main()

我收到以下错误:

Traceback (most recent call last):
  File "C:/Users/Simon/Documents/import_landsat2.py", line
40, in <module>
    main()
  File "C:/Users/Simon/Documents/import_landsat2.py", line
37, in main
    import_tifs(sys.argv[1])
  File "C:/Users/Simon/Documents/import_landsat2.py", line
17, in import_tifs
    for file in os.listdir(dirname):
WindowsError: [Error 3] The system cannot find the path
specified: 'dirpath\\C/*.*'

谁能解释正在发生的事情以及我需要做些什么来解决它,或提出替代方案?谢谢。

4

2 回答 2

2

我相信您的主要问题是dirnameinos.walk()返回一个列表(不是字符串),因此您的后续字符串(即dirname = os.path.join(dirpath,dirname))格式有点错误。这是一种可能的替代方法 - 为了对此进行测试,我使用了目录的完整路径 as sys.argv[1],但您可以使其更加动态以适合您的情况。此外,避免使用变量名称,例如file因为它们是 Python 关键字。我无法测试您的grass.*功能,但希望这将是一个足够清晰的示例,以便您可以调整您的需要。os.walk()本机处理许多标准解析,因此您可以删除一些目录操作函数:

def import_tifs(dirpath):
  for dirpath, dirname, filenames in os.walk(dirpath):
    # Iterate through the files in the current dir returned by walk()
    for tif_file in filenames:
      # If the suffix is '.TIF', process
      if tif_file.upper().endswith('.tif'):
        # This will contain the full path to your file
        full_path = os.path.join(dirpath, tif_file)

        # tif_file will already contain the name, so you can call from here
        grass.message('Importing %s -> %s@%s...' % (full_path, tif_file, dirpath))

        grass.run_command('r.in.gdal',
                          flags = 'o',
                          input = full_path,
                          output = tif_file,
                          quiet = True,
                          overwrite = True)
于 2012-10-25T17:56:42.277 回答
2

我刚刚重写了您的代码以列出所有目录树并查找文件扩展名,在本例中为“.tif”,

#!/usr/bin/python

import os
import sys


def import_tifs(dirpath):

    for dirpath, dirname, filenames in os.walk(dirpath):
        for filename in filenames:
            name, extension = os.path.splitext(filename)
            if extension.lower() == ".tif":
                filepath = os.path.join(dirpath, filename)

                print(filepath, name, dirpath)


def main():
    if len(sys.argv) == 1:
            import_tifs(os.getcwd())
    else:
        import_tifs(sys.argv[1])


if __name__ == "__main__":
    main()

请检查这是否是您正在寻找的...

于 2012-10-25T18:04:45.033 回答