Im trying to navigate directories in python like this, and for some reason its telling me that
IOError: File ../Harvard%20Stats%20Course%20Data%20Files/diamonds.csv does not exist. The file does exist, however, one folder out, and then in the Harvard Stats Course Data Files folder.
diamond_data = pd.read_csv('../Harvard Stats Course Data Files/diamonds.csv',
sep = ',', index_col=0)`
Any advice would be appreciated.
At the IPython prompt, type pwd. That will show you the current working directory. Perhaps it is not the directory you think it is. You can change the current workding directory by typing cd /path/to/dir at the IPython prompt.
Alternatively, you could simply supply an absolute path to the CSV file.
Using ../ like that will be relative to wherever you're running the program from. Try to change into the directory of the .py file and run it again.
Related
I am trying to use a repository from this link
https://github.com/zarroboogs/p4g-saveconv#moving-converted-saves-to-ps-vita
I've managed to successfully clone the repository and it instructs you to run the command
python convert_vita2pc.py [--custom-diff {disable,enable}] save_dir
but when I do I get the error
Python cannot open file ('Directory'): No such file or directory
Could someone help and teach me how to properly do this?
Looks like that Python script cannot find the direcotry named 'Directory' (I assume 'Directory' is the <save_dir> from guilde on that link you provided).
So maybe you can try to add an absolute path to that directory (on linux machines go in the desired directory ('Directory') and type 'pwd' command. Then copy the path to the <save_dir> place at the end of that convert script).
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.
I am currently reading the book Learn Python the Hard Way by Zed Shaw. On exercise 1 after learning the basics of Powershell we must open a notepad++ saved .py file in Powershell. Now here is the problem I'm having.
I am supposed to open this python file by running this command:
PS C:\Users\Trevor> python ex1.py
Zed Shaw does this in his book and it prints back what the file contains...("hello world")
Now i understand my path is wrong by the error message i receive telling me that python is not recognized. I have tried many many times to get the correct path to open python. I have saved the python27 file to my desktop and just about everything trying to get a path that will work.
I am starting at:
PS C:\Users\Trevor>
Any idea how to get to my python27 file and open python when it is saved to my desktop? I also have my ex1.py file saved to my python27 folder. Should i remove it? please help if you can thanks!
PowerShell cannot find python in the current directory or in the directories specified in PATH environment variable.
You can add your installed python directory to PATH variable in System Properties (Win+R → type in sysdm.cpl) → Advanced → Environment variables... → Under "user variables for ...", find PATH variable (if there aren't any, create it using New...), double click to edit it and insert "<your python path>;" (without the quotes). After that, restart PowerShell, run your command and you're done.
I also had this problem, but for me the solution to it wasn't creating a path. I typed in the same code as you, and this is the error message I got:
C:\Users\UserName\AppData\Local\Programs\Python\Python36\python.exe: can't open file 'ex1.py': [Errno 2] No such file or directory
The fix to this was a lot easier than I expected it to be, but it took me a little bit longer to figure out what it was since I just started learning how to work with Python and PowerShell.
This is what you need to do to solve this issue:
Instead of typing the name of the file you are trying to open, you first need to open the directory (the folder where all of your python files are stored).
To open the directory, type in cd DirectoryName and then press enter.
After that, type in "python" (without the quotes) and then the file name (for example, ex1.py). Then, press enter.
At this point, PowerShell should be able to open the file.
This is what you should see:
PS C:\Users\UserName> cd DirectoryName
PS C:\Users\UserName\DirectoryName> python FileName.py
(Note: This is when the file would print)
If your file still isn't opening, you may need to be more specific about the path to the directory. I have my directory located in C:\Users\UserName. If your directory is in C:\Users\UserName but you have it embedded within another folder, you may have to open that folder in PowerShell (you can do this by typing cd FolderName) before you can open the directory folder that's inside it. The easiest thing to do is to save your directory folder in the path C:\Users\UserName so that opening files will take less time/typing.
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