Kosher Moses Kosher Moses - 1 month ago 16
Python Question

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 all of the necessary constants so that the script can create and save the pertinent files
# on the users desktop

src_dataset = Tk()
srcVH = Tk()
gdalTranslate = 'C:\Program Files (x86)\GDAL\gdal_translate.exe'
src_dataset.fileName = filedialog.askopenfilename(title="Select HV File")
srcVH.fileName = filedialog.askopenfilename(title="Select VH File")
dst_dataset = os.path.expanduser('~\\Desktop\\Components\\Float32\\newHV32.img')
dstVH = os.path.expanduser('~\\Desktop\\Components\\Float32\\newVH32.img')
cmd = "-ot float32 -of HFA" # hopefully this works

# Create new folders which will house the files that will be created
# along each sequential step of the script

newpath = os.path.expanduser('~\\Desktop\\Components\\Float32')
if not os.path.exists(newpath):
os.makedirs(newpath)

# Run gdal_translate utilizing the original HV and VH polarization images
# then place them as float32 type within a Float32 folder on the users desktop

hvfullCmd = ' '.join([gdalTranslate, cmd, src_dataset.fileName, dst_dataset])
subprocess.call(hvfullCmd)
vhfullCmd = ' '.join([gdalTranslate,cmd,srcVH.fileName,dstVH])
subprocess.call(vhfullCmd)

Answer

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