1

I have a problem for apply this function in all files. This script is about the extraction of the bands from a raster and I want to use it on all the file raster in the folder. It works on the single file but I can not apply to all files from the folder.

Why?

import glob, os, processing
from osgeo import gdal
in_path="C:/.../folder_input/"
out_path ="C:/.../folder_output/"

file_list = os.listdir(path)

file_list= [file for file in os.listdir(path) if os.path.splitext(file)[1] == '.ecw']

for file in file_list:
print (file)

src_ds = gdal.Open(file)

for file in range (1,file.RasterCount+1):
out_ds = gdal.Translate(out_path + '1' + str(file) + 'ecw',src_ds, format='GTiff', bandList=[i])
out_ds=None

AttributeError: 'str' object has no attribute 'RasterCount'

1

First you'll have to define a function and make the file(s) in the directory that you're trying to work with an argument.

>>> def function_name(files):
#this is where you write what you want your function to do...
out_ds = gdal.Translate(out_path + '1' + str(files) + 'ecw',src_ds, format='GTiff', bandList=[i])
out_ds=None
>>> for file in os.listdir(path): #this will loop through the path and get each file.
function_name(file) #this is where you pass the file to the function.
  • I tried but it returns AttributeError: 'str' object has no attribute 'RasterCount'. You could explain me better, I'm not practical enough with python and programming.. – vins_26 Mar 7 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.