How to know/change current directory in Python shell?

PythonWindowsPython 3.xPython 3.2

Python Problem Overview


I am using Python 3.2 on Windows 7. When I open the Python shell, how can I know what the current directory is and how can I change it to another directory where my modules are?

Python Solutions


Solution 1 - Python

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 imported 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

Solution 2 - Python

you want

import os
os.getcwd()
os.chdir('..')

Solution 3 - Python

>>> 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.

Solution 4 - Python

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")

Solution 5 - Python

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.

Solution 6 - Python

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

Solution 7 - Python

You can try this:

import os

current_dir = os.path.dirname(os.path.abspath(__file__))   # Can also use os.getcwd()
print(current_dir)                                         # prints(say)- D:\abc\def\ghi\jkl\mno"
new_dir = os.chdir('..\\..\\..\\')                         
print(new_dir)                                             # prints "D:\abc\def\ghi"


Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionastay13View Question on Stackoverflow
Solution 1 - Pythonwal-o-matView Answer on Stackoverflow
Solution 2 - PythonsimonView Answer on Stackoverflow
Solution 3 - Pythonshankar_pratapView Answer on Stackoverflow
Solution 4 - PythonsambeetView Answer on Stackoverflow
Solution 5 - PythonSteven RumbalskiView Answer on Stackoverflow
Solution 6 - PythondeontologicianView Answer on Stackoverflow
Solution 7 - PythonAditya N.SView Answer on Stackoverflow