pythonpandaspyqgis

An undefined letter appears?


I'm running the following snippet. For some reason, which I don't get, a "b" appears in the path when stating the location of a file.

# %% Definitions

path_root = normpath(r'G:\\Dropbox\\temp\\')
path_start = normpath(r'G:\\Dropbox\\temp\\worklayers\\')
path_temp = normpath(r'G:\\Dropbox\\temp\\templayers\\')
path_temp2 = normpath(r'G:\\Dropbox\\temp\\temp2layers\\')
path_end = normpath(r'G:\\Dropbox\\temp\\resultlayers\\')
path_doc = normpath(r'G:\\Dropbox\\Documents\\')

directory = os.fsencode(path_start)

df = pd.read_csv(path_doc+'\\DEM_masterfile.csv')

slope_list = [4, 4.9, 5.9, 7.1, 8.2, 10, 11.8, 13.8, 15.7, 17.9, 20.6, 25]
aspect_list_1 = [[0, 15], [15,30], [30, 45], [45, 60], [60, 75], [75, 90], [90, 105], [105, 120], [120, 135], [135, 150], [150, 165], [165, 180]]
aspect_list_2 = [[180, 195], [195, 210], [210, 225], [225, 240], [240, 255], [255, 270], [270, 285], [285, 300], [300, 315], [315, 330], [330, 345], [345, 360]]

# %% Generating the slope and aspect vector files from the DEM raster

for number in os.listdir(directory):
    
    processing.run("native:slope", {'INPUT':path_start+'\\'+str(number),'Z_FACTOR':1,'OUTPUT':path_temp+'\\'+'slope_'+str(number)})
    processing.run("native:aspect", {'INPUT':path_start+'\\'+str(number),'Z_FACTOR':1,'OUTPUT':path_temp+'\\'+'aspect_'+str(number)})

When running this the following error appears Could not load source layer for INPUT: G:\Dropbox\temp\worklayers\b'hej0.tif' not found

Where is that b in the error message coming from?? G:\Dropbox\temp\worklayers\b'hej0.tif'


Solution

  • os.fsencode(path_start) returns an instance of bytes. Then os.listdir(directory) returns a list of bytes because you gave bytes as its argument.

    The b'...' is the result of calling str(...) on an instance of bytes. I'm not sure what the reason was for using os.fsencode in this code and ending up with bytes.

    To convert a bytes to a string, you could use decode() or os.fsdecode, the reverse of os.fsencode.