"pwd" gives the path to the windows machine when starting the python subprocess

I wrote the following script called file_I_executed.py:

import subprocess
def main():
    loc = subprocess.popen("pwd")
    print loc

Which gave the result:

C:\Python27\python.exe C:/Users/username/PycharmProjects/projectname/file_I_executed.py

However, when I try "pwd" on cmd windows, I get:

C:\Users\username\somedirectory>pwd
'pwd' is not recognized as an internal or external command,
operable program or batch file.

What gives? How does the subprocess work? Why does "pwd" give me the python path, as well as the script path, when it obviously should not give me this, when I run this from the Windows command line?

I am using python 2.7.1 from pycharm, on windows 7.

CLARIFICATION: I fully understand that "pwd" is not a Windows command. However, the above script gave me the result that I indicated, and I do not understand why.

+4
source share
3 answers

, PyCharm, subprocess.popen("pwd"). , !

main, , main().

- PyCharm . . , .

main() , subprocess.popen("pwd"), .

subprocess.popen("pwd"), , pwd. ( anarchos78 .)

PyCharm , . , , .

+3

pwd - linux, Windows. Windows echo %cd%.

+1

: https://docs.python.org/3/library/subprocess.html#module-subprocess

, Popen ( ) . . , , :

args is required for all calls and must be a string or sequence of program arguments. Usually a sequence of arguments is suggested, since it allows the module to take care of any required escaping and quoting of arguments (for example, allow spaces in file names). If you pass a single line, either the shell should be True (see below), or the line should simply indicate the executable program without specifying any arguments.

However, this is odd, as the call to Popen should be uppercase, as a recent comment said.

+1
source

All Articles