我的同事在安装 Python 时遇到问题。当运行下面的代码时,从'C:\my\folder\'
, 'C:\'
返回而不是当前工作目录。当我或其他人在我们的系统上运行脚本时,我们得到'C:\my\folder\'
.
我们假设一定是某些全局设置导致了该问题,因此我让人卸载了 Python,删除了本地 Python2.7 文件夹,清理了注册表并重新安装了 Python,但它仍然无法正常工作。
注意:我们有大量遗留脚本,因此修改所有脚本以使用子流程是不切实际的。 :(
有任何想法吗?
环境:Windows XP、Python 2.7
import os
#
# This test script demonstrates issue on the users computer when python invokes
# a subshell via the standard os.system() call.
#
print "This is what python thinks the current working directory is..."
print os.getcwd()
print
print
print "but when i execute a command *from* python, this is what i get for the current working directory"
os.system('echo %cd%')
raw_input()
你也可以尝试这样的事情
os.chdir("C:\\to\\my\\folder")
print os.system("echo %CD%")
raw_input()
为了获取当前工作目录,我使用了不同的方法
cur_dir = os.path.abspath(".")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)