There are multiple inconsistencies with your original code so I'll try to adress them one by one:
It is considered good Python form to only import what you actually use, in your case that would be
from osgeo import gdal
os.listdir returns a list of filenames in your
path3. In order to perform any operation based on that list you need to move your code inside the loop. In your case only the last file will be stored in
filename and passed to
gdal.Open instead of all of them one after another. Additionally you need to pass the full path to the file instead of just the filename, unless the files are in your current working directory.
path3 = 'D:/state_subsets'
for filename in os.listdir(directory):
g = gdal.Open(os.path.join(path, filename), GA_ReadOnly)
# insert other calculations here
g = None
In your example
g likely produces a
None output because it tries to open
filename but is not able to find it in the current working directory.