1

I have trouble when I try to set null some value, when I try this code, it didn't do anything or exporting any file. I run this code on Python ArcGIS Pro and when I import the ArcPy and os it seems ok.

import arcpy
import os
#inputcloudmask
arcpy.env.workspace = "E:\aa\TUGAS AKHIR\VNP46\2018\1 Juni 2018\QF Cloud Mask"

path = "E:\aa\TUGAS AKHIR\VNP46\2018\1 Juni 2018\QF Cloud Mask"

raw_name = os.listdir(path)
rname: str
outname = []
for rname in raw_name:
    portion = os.path.splitext(rname)
    temp_name = 'filter' + portion[0] + '.TIF'
    outname.append(temp_name)

rasters = arcpy.ListRasters("QF*","TIF")
i = 0
for raster in rasters:
    Oname = outname[i]
    i = i + 1
    outSetNull = arcpy.sa.SetNull(raster, 1, "VALUE <> 32 And VALUE <> 34 And VALUE <> 36 And VALUE <> 42 And VALUE <> 48 And VALUE <> 50 And VALUE <> 52 And VALUE <> 58 And VALUE <> 1056 And VALUE <> 1058 And VALUE <> 1060 And VALUE <> 1066 And VALUE <> 1072 And VALUE <> 1074 And VALUE <> 1076 And VALUE <> 1082")
    on = "E:/aa/TUGAS AKHIR/VNP46/2018/1 Juni 2018\output" + Oname
    outSetNull.save(on)
    print(raster + "---->" + Oname)

This is the file in my path (E:\aa\TUGAS AKHIR\VNP46\2018\1 Juni 2018\QF Cloud Mask)

file in my path

What is wrong with my code?

1 Answer 1

2

Try adding an r before each string containing the absolute path to the file/directory. Without the r, Python interprets several escape sequences such as \t, \n and in your case, \a.

For example, run this as an excercise:

>>> print("\a")  # doesn't print anything

>>> print(r"\a")  # prints the literal string
\a

With your paths, there is also a difference:

>>> print("E:\aa\TUGAS AKHIR\VNP46\2018\1 Juni 2018\QF Cloud Mask")
E:a\TUGAS AKHIR\VNP468 Juni 2018\QF Cloud Mask
>>> print(r"E:\aa\TUGAS AKHIR\VNP46\2018\1 Juni 2018\QF Cloud Mask")
E:\aa\TUGAS AKHIR\VNP46\2018\1 Juni 2018\QF Cloud Mask

Furthermore, I would strongly recommend avoiding whitespaces in paths as they can cause other type of issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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