How to Know/Change Current Directory in Python Shell

How to know/change current directory in Python shell?

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

Equivalent of shell 'cd' command to change the working directory?

You can change the working directory with:

import os

os.chdir(path)

There are two best practices to follow when using this method:

  1. Catch the exception (WindowsError, OSError) on invalid path. If the exception is thrown, do not perform any recursive operations, especially destructive ones. They will operate on the old path and not the new one.
  2. Return to your old directory when you're done. This can be done in an exception-safe manner by wrapping your chdir call in a context manager, like Brian M. Hunt did in his answer.

Changing the current working directory in a subprocess does not change the current working directory in the parent process. This is true of the Python interpreter as well. You cannot use os.chdir() to change the CWD of the calling process.

Find the current directory and file's directory

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)


To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

change current working directory in python

I think a few things may be helpful.

It looks like you're on a windows system, so you should use double back slashes '\\' to separate the folders.

Second, if you're trying to change to a folder within the current folder, you should use a single dot, and not two, e.g. os.chdir('.\\folder')

Finally, if the folder you are trying to access is not a direct subfolder of the current working directory (or otherwise in your path), you need to include the full path to access it. Since you said it's on your desktop, you'd probably want something that looks like this:

import os
os.chdir('C:\\Users\\username\\Desktop\\headfirstpython') ## Where username is replaced with your actual username

From here, you could also change directories to the chapter3 subdirectory with the following

os.chdir('chapter3') 

Which is equivalent in this case with

os.chdir('.\\chapter3')

or, if you want to be wordy:

os.chdir('C:\\Users\\username\\Desktop\\headfirstpython\\chapter3')

Hopefully that helps?

Change current directory and view available directories

import os

os.chdir('folder1')

or

os.chdir('folderinfolder1')

How to set the current working directory?

Try os.chdir

import os
os.chdir(path)

        Change the current working directory to path. Availability: Unix, Windows.

What's the working directory when using IDLE?

You can easily check that yourself using os.getcwd:

>>> import os
>>> os.getcwd()
'C:\\Program Files\\Python33'

That’s on my Windows machine, so it’s probably the installation directory of Python itself.

You can change that directory at runtime using os.chdir:

>>> os.chdir('C:\\Users\\poke\\Desktop\\')
>>> os.getcwd()
'C:\\Users\\poke\\Desktop'
>>> with open('someFile.txt', 'w+') as f:
f.write('This should be at C:\\Users\\poke\\Desktop\\someFile.txt now.')

This will—not surprisingly—create the file on my desktop.

Change working directory from python or shell script

Since python runs in its own process, it won't be able to change the current directory of your shell. However, you could do something like this:

change_path() {
# prog.py figures out the real path that you want and prints
# it to standard output
local new_path=$(python prog.py some_path1) # could use an argument "$1"
cd "$new_path"
}

How to change working directory in Jupyter Notebook?

Running os.chdir(NEW_PATH) will change the working directory.

import os
os.getcwd()
Out[2]:
'/tmp'
In [3]:

os.chdir('/')
In [4]:

os.getcwd()
Out[4]:
'/'
In [ ]:


Related Topics



Leave a reply



Submit