我的同事在安装Python时遇到问题。运行以下代码时'C:\my\folder\','C:\'将返回from ,而不是当前的工作目录。当我或其他人在系统上运行脚本时,我们得到'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()
Run Code Online (Sandbox Code Playgroud)
你也可以尝试这样
os.chdir("C:\\to\\my\\folder")
print os.system("echo %CD%")
raw_input()
Run Code Online (Sandbox Code Playgroud)
也要获得当前的工作目录,我使用不同的方法
cur_dir = os.path.abspath(".")
Run Code Online (Sandbox Code Playgroud)