将环境变量评估为字符串 [英] Evaluate environment variables into a string

查看:137
本文介绍了将环境变量评估为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表示路径的字符串。由于此应用程序在Windows,OSX和Linux上使用,因此我们定义了环境变量,以正确地映射来自不同文件系统的卷。结果是:

 $ C / test / testing

我想做的是评估字符串中的环境变量,以便它们被各自的卷名称替换。有没有一个特定的命令我缺少,或者我必须采取os.environ.keys()并手动替换字符串?

解决方案

使用 os.path.expandvars 展开字符串中的环境变量,例如:

 >>> os.path.expandvars('$ C / test / testing')
'/ stackoverflow / test / testing'


I have a string representing a path. Because this application is used on Windows, OSX and Linux, we've defined environment variables to properly map volumes from the different file systems. The result is:

"$C/test/testing"

What I want to do is evaluate the environment variables in the string so that they're replaced by their respective volume names. Is there a specific command I'm missing, or do I have to take os.environ.keys() and manually replace the strings?

解决方案

Use os.path.expandvars to expand the environment variables in the string, for example:

>>> os.path.expandvars('$C/test/testing')
'/stackoverflow/test/testing'

这篇关于将环境变量评估为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆