【问题标题】:Using os.path.expanduser When Username Has a Space当用户名有空格时使用 os.path.expanduser
【发布时间】:2016-11-03 10:46:16
【问题描述】:

我希望使用 [os.path.expanduser] 在我自己以外的计算机上运行各种 GDAL 进程。但是,在单独的 PC 上运行测试时,我遇到了用户名带有空格而不是 C:\Users\Kosher_Moses 的问题,用户名是 C​​:\Users\Kosher Moses。关于如何强制脚本解决这个问题的任何想法?

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

【问题讨论】:

  • 不要使用基于字符串的调用。为命令传递list 的参数不会有问题,因为离散参数不需要担心空格。阅读subprocess 文档,他们有很多例子。
  • 感谢您的指导。

标签: python operating-system subprocess gdal


【解决方案1】:

不要这样做:

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

【讨论】:

  • 如果我想使用类似的方法在没有 tkinter 的情况下运行 gdal_calc,以便脚本自动选择文件,我该怎么做?我已经添加了我在上面的问题中提到的代码行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-07
  • 1970-01-01
  • 2010-09-17
相关资源
最近更新 更多