get working directory in python

2 June, 2006 - 16:14
Categories:

This is mainly a remainder to myself, because I always forget it and spend to much time to find it again. It's so simple/basic that it is almost embarrassing.
To get the current working directory of in a python script, like you have pwd (print working directory) on a Linux/Unix command line:

import os
print os.getcwd()
8 May, 2012 - 16:07

so very nice :)

Anonymous (not verified)

so very nice :)

25 October, 2011 - 14:10

Another method

Stephen (not verified)

import subprocess

subprocess.Popen('pwd', shell = True)

this allows you to use bash commands in Python, so handy for changing directory's, copying files etc

3 January, 2012 - 15:39

Terrible idea

Anonymous (not verified)

Yeah, that's a horrible idea.

You also can't change directory like that. Popen makes a new process, and if that new process changes directory, it has no impact on the process that launched it.

6 February, 2011 - 00:10

Great. thank you

angel (not verified)

It worked fine for me, I'm a learning Python. hanks a lot

18 June, 2010 - 15:32

And you can always >>>

Anonymous (not verified)

And you can always

>>> fin,fout = os.popen4("pwd")
>>> result = fout.read()
>>> print result

6 July, 2011 - 21:48

You can always do that if you

Anonymous (not verified)

You can always do that if you want a non-portable solution that will crash on any system without a 'pwd' command.

This is exactly what you don't have to deal with if you use os.getcwd()

13 June, 2010 - 12:49

everyman...

albruno (not verified)

everyman needs their own wiki...
thanks for sharing yours!

3 May, 2010 - 22:39

It works! thank you

Anonymous (not verified)

It works! thank you

31 March, 2010 - 06:07

Thank you.

Anonymous (not verified)

Thank you.

8 January, 2010 - 13:10

Thanks a lot! Didn't know

Ambar (not verified)

Thanks a lot!
Didn't know that it's this easy!!!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed
  • Lines and paragraphs break automatically.

More information about formatting options