3

In my terminal if I run: echo $(pwd), I got /home/abr/workspace, but when I tried to run this script in python like this:

>>> import subprocess
>>> cmd = ['echo', '$(pwd)']
>>> subprocess.check_output(cmd, shell=True)

I get '\n'. How to fix this?

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
msn
  • 437
  • 1
  • 5
  • 11
  • 1
    Why would you ever do this instead of using the os package? Seems much easier to use built in functions instead of relying on external calls that are almost definitely os/privilege/system/configuration/terminal dependent. os.path.abspath(os.path.curdir) seems simple enough – en_Knight Mar 04 '16 at 15:19
  • if you hover the mouse over [tag:subprocess] under the question text; you should see the tag's description. Click `info` link under the description. [I've collected questions that are asked again and again there. If you study the questions; you might avoid common issues with using `subprocess` module in the future](http://stackoverflow.com/tags/subprocess/info). – jfs Mar 05 '16 at 19:23

4 Answers4

1

Use os package:

import os
print os.environ.get('PWD', '')
zondo
  • 19,901
  • 8
  • 44
  • 83
Karol
  • 301
  • 1
  • 8
1

Try this:

cmd = 'echo $(pwd)'
subprocess.check_output(cmd, shell=True)

In subprocess doc it specified that cmd should be a string when shell=True.
From the documentation:

The shell argument (which defaults to False) specifies whether to use the shell as the program to execute. If shell is True, it is recommended to pass args as a string rather than as a sequence.


A better way to achieve this is probably to use the os module from the python standard library, like this:

import os
print os.getcwd()
>> "/home/abr/workspace"

The getcwd() function returns a string representing the current working directory.

Forge
  • 6,538
  • 6
  • 44
  • 64
  • This is correct. If you think it adds anything, I'd consider adding that python has built in alternatives which are preferable, or to quote the docs on subprocess: " However, note that Python itself offers implementations of many shell-like features" – en_Knight Mar 04 '16 at 15:31
  • @msn please consider accepting the answer if it was helpful by clicking the check mark. – Forge Mar 06 '16 at 20:15
1

From the documentation on the subprocess module:

If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself.

You want:

subprocess.check_output("echo $(pwd)", shell=True)
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
1

The command subpreocess.check_output will return the output of the command you are calling: Example:

#echo 2
2

from python

>>>subprocess.check_output(['echo', '2'], shell=True)
>>>'2\n'

the '\n' is included because that is what the command does it prints the output sting and then puts the current on a new line.

now back to your problem; assuming you want the output of 'PWD', first of all you have to get rid of the shell. If you provide the shell argument, the command will be run in a shell environment and you won't see the returned string.

subprocess.check_output(['pwd'])

Will return the current directory + '\n'

On a personal note, I have a hard time understanding what you are trying to do, but I hope this helps solve it.

Pandrei
  • 4,843
  • 3
  • 27
  • 44