I'm trying to get the output of pwd
:
#!python
import os
pwd = os.system("pwd")
print (pwd)
it prints 0 which is the successful exit status instead the path. How can i get the path instead?
I'm trying to get the output of pwd
:
#!python
import os
pwd = os.system("pwd")
print (pwd)
it prints 0 which is the successful exit status instead the path. How can i get the path instead?
要获取当前工作目录,请使用os.getcwd()。但一般来说,如果你想要输出和返回码:
import subprocess
PIPE = subprocess.PIPE
proc = subprocess.Popen(['pwd'], stdout=PIPE, stderr=PIPE)
out, err = proc.communicate()
errcode = proc.returncode
print(out)
print(errcode)
你不能用 来做到这一点os.system
,尽管os.popen
确实会为你返回那个值:
>>> import os
>>> os.popen('pwd').read()
'/c/Python27\n'
但是,该subprocess
模块功能更强大,应改为使用:
>>> import subprocess
>>> p = subprocess.Popen('pwd', stdout=subprocess.PIPE)
>>> p.communicate()[0]
'/c/Python27\n'
当前目录完整路径:
import os
print os.getcwd()