Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - I'm trying to get the output of pwd and instead i get the exit status [duplicate]

Tags:

python

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?

like image 232
Itai Ganot Avatar asked Nov 29 '22 13:11

Itai Ganot


1 Answers

Running system commands is better done with subprocess, but if you are using os already, why not just do

pwd = os.getcwd()

os.getcwd() is available on Windows and Unix.

like image 82
Lev Levitsky Avatar answered Dec 06 '22 18:12

Lev Levitsky