当用户名有 Space 时使用 os.path.expanduser

Using os.path.expanduser When Username Has a Space

我想在我自己以外的计算机上使用 [os.path.expanduser] 到 运行 各种 GDAL 进程。但是,当运行在单独的PC上进行测试时,我运行进入了一个用户名用space而不是C:\Users\Kosher_Moses的问题,用户名是C :\Users\Kosher摩西。知道如何强制脚本解决这个问题吗?

# 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)

不要做:

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

做:

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