- 论坛徽章:
- 4
|
本帖最后由 linux_c_py_php 于 2012-09-25 12:16 编辑
楼主所说的调用gedit是指启动这个gedit这个编辑器, 是吗? (比如, 通过subprogress等.)
那么你可以预先为子进程设置环境变量, 以便子进程能够继承环境变量, 比如:- os.putenv('LC_ALL', 'en_US.utf8')
- subprogress.call(.....)
复制代码 这一点行为和C里的putenv以及环境变量的继承机制有点不同, 是Python自实现的一种工作方式, 也就是putenv不会影响当前进程的环境变量, 但子进程却能够使用这些环境变量.
os.putenv(key, value)
Set the environment variable named key to the string value. Such changes to the environment affect subprocesses started with os.system(), popen() or fork() and execv().
Availability: most flavors of Unix, Windows.
Note
On some platforms, including FreeBSD and Mac OS X, setting environ may cause memory leaks. Refer to the system documentation for putenv.
When putenv() is supported, assignments to items in os.environ are automatically translated into corresponding calls to putenv(); however, calls to putenv() don’t update os.environ, so it is actually preferable to assign to items of os.environ.
这段代码可以测试这个行为:- [liangdong@bb-browser-test00.vm.baidu.com py_project]$ cat main.py
- #python3.2
- #coding=utf8
- import os
- #os.putenv('LC_ALL', 'en_US.utf8')
- os.environ['LC_ALL'] = 'en_US.utf8'
- print(os.getenv('LC_ALL'))
复制代码 |
|