In an IPython nb, is it possible to set programmatically the working directory to the directory of the notebook?
For example, the following code will work in a regular .py file.
import os
# show working dir
os.chdir(os.path.abspath('/'))
print "initial working directory:\t", os.getcwd()
# get path of script file
scriptPath = os.path.abspath(os.path.dirname(__file__))
# change working dir to dir with script file
os.chdir(scriptPath)
# show working directory
print "final working directory:\t", os.getcwd()
However, I can't find the equivalent of the
__file__
variable for a ipython nb file. Is there some equivalent approach for ipynb files?
iPython notebook appears to automatically switch the directory to the same one as the .ipynb file. Do you want to change out of that directory and then change back later? If so, just store the original directory at the start of the program and use it whenever.
import os
orig_dir = os.getcwd()
os.chdir('/some/other/directory')
#do stuff
os.chdir(orig_dir)
EDIT: There appears to be a special variable _dh, which is a list such that _dh[0] contains the name of the directory in which the iPython kernel was started. I only just discovered this, so I'm not sure that this will be robust to a SaveAs either (I can't find a way to do this in my version of iPython). However, it doesn't change when I do os.chdir(), so I suspect that at least the first element of the list always contains the notebook's directory.
Related
I am learning pathlib in Python.
I create a script, printcwd.py:
from pathlib import Path
mypath = Path()
print (mypath.cwd())
This will give me the result I expect if I run the python script by double clicking it inside any folder - it will print the path of that folder as the cwd.
However, if I run the script in my terminal in VSCode (or in powershell) it will always give me the cwd as whatever the current directory of the terminal is set to, rather than the location of the printcwd.py file.
For example,
If I placed the file in C:\ and ran it, it would print C:\ as the cwd().
However if I ran it in VSCode with the terminal set to C:\Otherfolder, it would run this:
PS C:\Otherfolder> &
C:/Users/name/AppData/Local/Programs/Python/Python39/python.exe
C:/printpath.py
and print: C:\Otherfolder, despite the .py file existing at C:\
So, what is happening here ?
CWD is a property of the terminal, not of the python process. When you double click the file, Windows creates a terminal inside the directory and runs the file. The cwd function just access this property.
If you want to get the directory the file is placed in, use Path(__file__).parent. This access uses the module magic variable that python creates to point to where the file is placed.
Set-up
I run a script on my computer, located in the directory Users/path/to/my/script.py.
In the script, I use the path to the script, e.g.,
sub_path = 'Users/path/to/my/'
os.chdir(sub_path + 'other_script/')
As you can see, I define sub_path in the code 'manually'.
Problem
I don't want to define the sub_path manually, I'd rather have Python do it for me.
I'm looking for something similar to the code I use to obtain the current working directory: os.getcwd(), but then a code to obtain the directory of the current file.
I mainly find answers similar to this one, which says,
os.path.abspath(os.path.dirname(__file__))
but in the Spyder & Anaconda set-up, this generates a NameError: name '__file__' is not defined.
What can I do?
You if you want to move back one folder/directory you use the .. in your file path.
os.chdir('../other_scripts/')
will work. You may fine it helpful to view this or the wiki.
If you want to move from where you currently are you can use './new_dir/'. If you want to automate how to find other files you may want to read here which says to use os.walk. This may be the same question.
Mark8888 pointed out to run the whole script (run file (F5)) instead of just pieces of the script
this way multiple approaches should work to get the script file location and change the current working directory
import os
# directory of script file
print(os.path.abspath(os.path.dirname(__file__)))
# change current working directory
os.chdir(os.path.abspath(os.path.dirname(__file__)))
# current working directory
print(os.getcwd())
also
import os
import sys
# directory of script file
print(os.path.abspath(os.path.dirname(sys.argv[0])))
# change current working directory
os.chdir(os.path.abspath(os.path.dirname(sys.argv[0])))
# current working directory
print(os.getcwd())
I add the following lines to any script I run in case I need to access data relative to the location of the script
import sys
script = sys.argv[0]
print(script)
'C:/SomeFolder/A_Subfolder/CurrentlyRunningScript.py' # changed obviously
First, save your Jupyter Notebook. Second, locate the directory your Jupyter Notebook is stored in. Thirdly, ensure that your Jupyter Notebook and CSV file are in the same place.
I couldn't find a place for me to change the working directory in Jupyter Notebook, so I couldn't use the pd.read_csv method to read in a specific csv document.
Is there any way to make it? FYI, I'm using Python3.5.1 currently.
Thanks!
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 [ ]:
You may use jupyter magic command as below
%cd "C:\abc\xyz\"
First you need to create the config file, using cmd :
jupyter notebook --generate-config
Then, search for C:\Users\your_username\.jupyter folder (Search for that folder), and right click edit the jupyter_notebook_config.py.
Then, Ctrl+F: #c.NotebookApp.notebook_dir ='' . Note that the quotes are single quotes. Select your directory you want to have as home for your jupyter, and copy it with Ctrl+C, for example: C:\Users\username\Python Projects.
Then on that line, paste it like this : c.NotebookApp.notebook_dir = 'C:\\Users\\username\\Python Projects'
Make sure to remove #, as it is as comment.
Make sure to double slash \\ on each name of your path.
Ctrl+S to save the config.py file !!!
Go back to your cmd and run jupyter notebook. It should be in your directory of choice. Test it by making a folder and watch your directory from your computer.
on Jupyter notebook, try this:
pwd #this shows the current directory
if this is not the directory you like and you would like to change, try this:
import os
os.chdir ('THIS SHOULD BE YOUR DESIRED DIRECTORY')
Then try pwd again to see if the directory is what you want.
It works for me.
it is similar to jason lee as he mentioned earlier:
in Jupyter notebook, you can access the current working directory by
pwd()
or by import OS from library and running os.getcwd()
i.e. for example
In[ ]: import os
os.getcwd( )
out[ ]: :c\\users\\admin\\Desktop\\python
(#This is my working directory)
Changing Working Directory
For changing the Working Directory (much more similar to current W.d just you need to change from os.getcwd() to os.chdir('desired location')
In[ ]: import os
os.chdir('c:user/chethan/Desktop') (#This is where i want to update my w.d,
like that choose your desired location)
out[ ]: 'c:user\\chethan\\Desktop'
It's simple, every time you open Jupyter Notebook and you are in your current work directory, open the Terminal in the near top right corner position where create new Python file in. The terminal in Jupyter will appear in the new tab.
Type command cd <your new work directory> and enter, and then type Jupyter Notebook in that terminal, a new Jupyter Notebook will appear in the new tab with your new work directory.
Jupyter under the WinPython environment has a batch file in the scripts folder called:
make_working_directory_be_not_winpython.bat
You need to edit the following line in it:
echo WINPYWORKDIR = %%HOMEDRIVE%%%%HOMEPATH%%\Documents\WinPython%%WINPYVER%%\Notebooks>>"%winpython_ini%"
replacing the Documents\WinPython%%WINPYVER%%\Notebooks part with your folder address.
Notice that the %%HOMEDRIVE%%%%HOMEPATH%%\ part will identify the root and user folders (i.e. C:\Users\your_name\) which will allow you to point different WinPython installations on separate computers to the same cloud storage folder (e.g. OneDrive), accessing and working with the same files from different machines. I find that very useful.
Open jupyter notebook click upper right corner new and select terminal then type cd + your desired working path and press enter this will change your dir. It worked for me
list all magic command %lsmagic
show current directory %pwd
I have did it on windows machine. Detail mentioned below
From windows start menu open “Anaconda Prompt
Find .jupyter folder file path .
In command prompt just type
or to find the .jupyter path
After find the .jupyter folder, check there has “jupyter_notebook_config” file or not. If it is not there then run below command
After run the command it will create "jupyter_notebook_config.py"
if do not have administrator permission then Some time you could not find .jupyter folder . Still you can open config file from any of the text editor
Open “jupyter_notebook_config.py” file from the the “.jypyter” folder.
After open the file need to update the directory is use for notebooks and kernel. There are so many line in config file so find “#c.NotebookApp.notebook_dir” and update the path
After:
Save the file
6. Now try to create or read some file from the location you set
What works for me on Windows is creating a shortcut to jupyterlab, and altering the properties of the shortcut.
At "Start in:", enter your desired working directory.
Using this method, you don't have to configure a global file either.
Saying I create a.py under path/to/somewhere/. And later in the IDE(pycharm), I refactor it to path/to/another_place/.
But when I run a.py later, os.getcwd() show its working directory is still in path/to/somewhere. ( os.listdir('.') also show it is in original place.)
The following picture is what I meet , makeimg.py is originally in learn_function
I am new to python and I don't understand how a file/modoule locate itself.
Am I do something wrong? or it is a bug?
os.getcwd() returns the current working directory, it may/may not be the directory in which the your script exists, its the directory from which the script was run. Most probably in your PyCharm Run/Debug configuration you have set the Working directory to path/to/somewhere .
From run/debug configuration doc page for pycharm -
Working directory - Specify a directory to be used by the running task.
When a default run/debug configuration is created by the keyboard shortcut Ctrl+Shift+F10 , or by choosing Run on the context menu of a script, the working directory is the one that contains the executable script. This directory may differ from the project directory.
Ideally your code should not depend on the current working directory , since you can run a python file from anywhere using absolute path to the python file.
Instead if you want the path in which the script resides, use __file__ to get that path in the script.
Example -
print(__file__)
This should print the script's path (as given in sys.argv[0]).
I'm new and I have no idea where the default directory for the open() function is.
For example open('whereisthisdirectory.txt','r')
Can someone advise me? I've tried googling it (and looking on stackoverflow) and even putting a random txt file in so many folders but I still can't figure it out. Since I'm beginning, I want to learn immediately rather than type "c:/directory/whatevevr.txt" every time I want to open a file. Thanks!
Ps my python directory has been installed to C:\Python32 and I'm using 3.2
os.getcwd()
Shows the current working directory, that's what open uses for for relative paths.
You can change it with os.chdir.
If you working on Windows OS first type
import os
then type
os.getcwd()
and it should print the current working directory.
The answer is not python-specific. As with programs written in any other language, the default directory is whatever your operating system considers the current working directory. If you start your program from a command prompt window, the CWD will be whatever directory you were in when you ran the program. If you start it from a Windows menu or desktop icon, the CWD is usually defined alongside the program's path when creating the icon, or else falls back to some directory that Windows uses in the absence of that information.
In any case, your program can query the current working directory by calling os.getcwd().
The default location is the CWD (Current Working Directory), so if you have your Python script in c:\directory and run it from there, if you call open() it will attempt to open the file specified in that location.
First, you must import:
import os
Then to print the current working directory:
os.getcwd()
If you want to change the current working directory:
os.chdir('your_complete_required_path')
create the .txt file in the directory where u have kept .py file(CWD) and run the .py file.
The open() function for file always creates files in the current working directory. The best way to find out your current working directory is to find three lines of small code:
import os
current_working_directory = os.getcwd()
print(current_working_directory)
Run this above code and you will get your current working directory where open() function creates a new file. Good Luck!
If you’re running your script through an interpreter (i.e pycharm, VSCode etc) your Python file will be saved, most likely, in my documents (at least in VSCode, in my personal experience) unless you manually save it to a directory of your choosing before you run it. Once it is saved, the interpreter will then use that as you current directory so any saves your Python script will create will also automatically go there unless you state otherwise.
it depends on how you run it from the terminal
like this, it is going to look in your home directory
C:\Users\name>python path\file.py
and like this, it is going to look next to your file
C:\Users\name>cd path
C:\Users\name\path>python file.py