getcwd() method. The os. getcwd() method is used for getting the Current Working Directory in Python. The absolute path to the current working directory is returned in a string by this function of the Python OS module.
The Python Command PromptUse "cd" to change your directory to the folder with the current version of Python you want to use (i.e. C:/Python26/ArcGIS10. 0). Type "dir" in this folder and you'll see "python.exe".
To change the current working directory(CWD) os. chdir() method is used. This method changes the CWD to a specified path. It only takes a single argument as a new directory path.
You can use the os
module.
>>> import os
>>> os.getcwd()
'/home/user'
>>> os.chdir("/tmp/")
>>> os.getcwd()
'/tmp'
But if it's about finding other modules: You can set an environment variable called PYTHONPATH
, under Linux would be like
export PYTHONPATH=/path/to/my/library:$PYTHONPATH
Then, the interpreter searches also at this place for import
ed modules. I guess the name would be the same under Windows, but don't know how to change.
edit
Under Windows:
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
(taken from http://docs.python.org/using/windows.html)
edit 2
... and even better: use virtualenv
and virtualenv_wrapper
, this will allow you to create a development environment where you can add module paths as you like (add2virtualenv
) without polluting your installation or "normal" working environment.
http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html
you want
import os
os.getcwd()
os.chdir('..')
>>> import os
>>> os.system('cd c:\mydir')
In fact, os.system()
can execute any command that windows command prompt can execute, not just change dir.
The easiest way to change the current working directory in python is using the 'os' package. Below there is an example for windows computer:
# Import the os package
import os
# Confirm the current working directory
os.getcwd()
# Use '\\' while changing the directory
os.chdir("C:\\user\\foldername")
Changing the current directory is not the way to deal with finding modules in Python.
Rather, see the docs for The Module Search Path for how Python finds which module to import.
Here is a relevant bit from Standard Modules section:
The variable sys.path is a list of strings that determines the interpreter’s search path for modules. It is initialized to a default path taken from the environment variable PYTHONPATH, or from a built-in default if PYTHONPATH is not set. You can modify it using standard list operations:
>>> import sys
>>> sys.path.append('/ufs/guido/lib/python')
In answer your original question about getting and setting the current directory:
>>> help(os.getcwd)
getcwd(...)
getcwd() -> path
Return a string representing the current working directory.
>>> help(os.chdir)
chdir(...)
chdir(path)
Change the current working directory to the specified path.
If you import os
you can use os.getcwd
to get the current working directory, and you can use os.chdir
to change your directory
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With