Search code examples
pythonoperating-systemsubprocessgdal

Using os.path.expanduser When Username Has a Space


I'm looking to use [os.path.expanduser] to run various GDAL processes on a computer other than my own. However, when running a test on a separate PC, I ran into the issue of a user name with a space instead of C:\Users\Kosher_Moses, the username is C:\Users\Kosher Moses. Any idea on how I can force the script to move past this problem?

# Set varible for gdal_calc

gdal_calc = "C:\\Program Files (x86)\\GDAL\\gdal_calc.py"

# make dictionary of environmental variables
gdal_env = os.environ.copy()

# modify and add variables
gdal_env["GDAL_DATA"] = "C:\\Program Files (x86)\\GDAL\gdal-data"
gdal_env["GDAL_DRIVER_PATH"] = "C:\\Program Files (x86)\\GDAL\\gdalplugins"
gdal_env["PATH"] = gdal_env["PATH"] + ";C:\\Program Files (x86)\\GDAL"

# Set constants
# The pathway to the images files are nested within the '--outfile=' command

inHVZero = os.path.expanduser('~\\Desktop\\Components\\Zeros\\newHVZeros_.img')
outPlace = os.path.expanduser('~\\\Desktop\\Components\\db_Files\\newHVdB.img')
outVFile = '--outfile='+ outPlace
cmd_HV = ['-A', inHVZero, outVFile, '--calc=10*log10(power(A,2))-83']
#calc_cmd_HV = ['C:\\Program Files (x86)\\GDAL\\gdal_calc.py', '-A', inHVZero, '--outfile='+outPlace, '--calc=10*log10(power(A,2))-83']

inVHZero = os.path.expanduser('~\\Desktop\\Components\\Zeros\\newVHZeros_.img')
outPlace_1 = os.path.expanduser('~\\Desktop\\Components\\db_Files\\newVHdB.img')
outVFile_1 = '--outfile='+ outPlace_1
cmd_VH = ['-A', inVHZero, outVFile_1, '--calc=10*log10(power(A,2))-83']
#calc_cmd_VH = ['C:\\Program Files (x86)\\GDAL\\gdal_calc.py', '-A', inVHZero, '--outfile='+outPlace_1, '--calc=10*log10(power(A,2))-83']


subprocess.call([sys.executable,gdal_calc] + cmd_HV, env=gdal_env)
subprocess.call([sys.executable,gdal_calc] + cmd_VH, env=gdal_env)

Solution

  • Don't do:

    cmd = "-ot float32 -of HFA"
    hvfullCmd = ' '.join([gdalTranslate, cmd, src_dataset.fileName, dst_dataset])
    subprocess.call(hvfullCmd)
    

    Do:

    cmd = ['-ot', 'float32', '-of', 'HFA']
    subprocess.call([gdalTranslate] + cmd + [src_dataset.fileName, dst_dataset])