将环境变量评估为字符串

时间:2023-01-15 07:44:14

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:

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

"$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?

我想要做的是评估字符串中的环境变量,以便它们被各自的卷名替换。是否存在我缺少的特定命令,或者我是否必须使用os.environ.keys()并手动替换字符串?

1 个解决方案

#1


64  

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

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

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

#1


64  

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

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

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