open() method not working in JupyterLab ( pyspark kernel ) - python

Getting below error when running the command:
output = open( "C:/Users/TAA3656/mytddutc_nudges_sample.json", 'w') # Update to local path and file name
[Errno 2] No such file or directory: 'C:/Users/TAA3656/mytddutc_nudges_sample.json'
Traceback (most recent call last):
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/TAA3656/mytddutc_nudges_sample.json'

I'd guess the path C:/Users/TAA3656 doesn't exist, so it's not possible to create a file in this nonexistent path. For example:
>>> open("nonexistent/thing.json", 'w')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'nonexistent/thing.json'
One could think that open(..., 'w') should create nonexistent/thing.json if it doesn't exist, but in this case, the directory nonexistent is... non-existent, so open refuses to create the file along with an entire path structure.
You should create the path first:
from pathlib import Path
the_path = Path("C:/Users/TAA3656")
# Create the path if it doesn't exist
the_path.mkdir(parents=True, exist_ok=True)
# Open or create the file
with (the_path / "your_file.json").open("w") as output:
... # run your code

verify the path of the file you are trying to open is correct. to verft the path exist use !pwd to show the path you are in and navegate to the location and !mkdir name to create the folder and the you can acces it.you also need to import sys and import os. so you can navegate through the system files

Do you know if you are running your jupyterlab locally, or in some kind of cloud environment? (Your mention of a pyspark kernel suggests to me it may be the latter.) If it is a cloud platform of some kind, you may not have access to your local hard disk.
Try this to check your platform, and see if it looks local to you.
Or you can try:
import os
os.getcwd()
to give you the path of where you are currently working. If it doesn't look like a directory on your local pc, again, you not be working locally.
Or ask a friendly looking colleague.

Related

"No such file or directory" when writing to file with python

I've encountered a very bizarre error when trying to write files with python. As of today, whenever I try and open a file in write mode, python throws the error 'No such file or directory', even if the directory definitely exists. All other python functionality seems to be working, including reading files.
A simple example of the problem, when run using the command line from my documents:
>>> with open('test.csv', 'w') as f:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'test.csv'
I'm using a windows 10 machine, python 3.7, with conda environments specific to projects I'm working on.
I have tried:
Restarting
Creating a clean conda environment (conda create --name test python=3.7)
Running in base without a conda environment
Providing the path as an absolute rather than relative, i.e. C:/Users/<myname>/Documents/test.csv
Trying to run this in other directories
The only success I've had is when running the code with append mode. If I try and create a file as above using mode='a' it throws the error, however if I manually create an empty file, I can append into it.
I really have no idea how to resolve this, other than to completely remove anaconda and python and re-install.
try declare a variable for the file before 'with' statement:
file = open('test.csv', 'w')
with file as f:
# your code here

How to fix "No such file or directory: 'AppleStore.csv'"?

I have a csv file and I put it at D:\Subin\PYTHON\AppleStore.csv
Then I launch my Jupyter Notebook with the url: http://localhost:8888/notebooks/PYTHON/test.ipynb
I try to run this script:
open_file = open('AppleStore.csv')
Then I get the issue:
FileNotFoundError Traceback (most recent call last)
<ipython-input-25-7784a3a20685> in <module>
----> 1 open_file = open('AppleStore.csv')
FileNotFoundError: [Errno 2] No such file or directory: 'AppleStore.csv'
the test.ipynb is at same directory with AppleStore.csv But I dont understand why I got file not found issue.
Anyone have idea about this?
If you are running python from within Jupyter, your working directly is likely where Juptyer is installed. Within your notebook, use print("my path is ", os.getcwd()) to check what your working directory is. (credit to gelonida)
Either use the full path to the file, or change your current working directory to the folder where the file is. os.chdir(FOLDER PATH GOES HERE)
For both of the code samples in this answer, you'll need to first import os.

Opening a file in Python gives me errors

I am quite new to Python and I am having problems opening a file in Python.
I want to open a text file called 'coolStuff' in a Folder on my Desktop and this is how I type in the command but I still get an error message. The file exists and so I do not understand why I get that error message.
open("coolStuff.txt","r")
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
open("coolStuff.txt","r")
FileNotFoundError: [Errno 2] No such file or directory: 'coolStuff.txt'
If you want to simply supply a filename like coolStuff.txt without also providing a full directory, then you have to make sure that Python is running in the same directory as the file. If you aren't sure what directory Python is running in, try this:
import os
print(os.getcwd())
You have two options:
let's say your file is in C:\path\to\dir\coolStuff.txt
1.
open(r'C:\path\to\dir\coolStuff.txt','r')
2.
import os
os.chdir(r'c:\path\to\dir')
open('coolStuff.txt', 'r')
Because the file you want to open is not in the current directory.You can find the file 'coolStuff.txt' in the terminal and launch your python environment at the same directory.

python 3.4 windows 8.1 unable to create file in C:\

I'm trying to run a script in python3.4 & windows 8 .
It worked well with windows 7 but now, in windows 8.1 it can´t write a file in C: , may be access denied.
If I change the directory, for example User/xxx it works, but I don´t know how to do to 'write' in C:
with open('C:/xxxx.csv', 'r') as csvfile:
Traceback (most recent call last):
File "C:\Python34\Scripts_jmab\csv_to_shp.py", line 12, in <module>
urllib.request.urlretrieve(a,filename)
File "C:\Python34\lib\urllib\request.py", line 188, in urlretrieve
tfp = open(filename, 'wb')
OSError: [Errno 22] Invalid argument: 'C:/xxxxx.csv'
thanks
Due to security settings in Windows, users are not allowed to create files in the root C:\ folder, as some important system files reside there that could be attacked by viruses. While it is possible to disable this feature, the easiest (and safest) solution is to create a directory for your programs to put their output files in. C:\TEMP is a good location (you can create it if it doesn't already exist), or you can make another folder named according to your own choosing.

Python I/O cannot find file, but path seems OK

I am with a python script. I want to open a file to retrieve data inside. I add the right path to sys.path:
sys.path.append('F:\WORK\SIMILITUDE\ALGOCODE')
sys.path.append('F:\WORK\SIMILITUDE\ALGOCODE\DTW')
More precisely, the file file.txt I will open is in DTW folder, and I also add upper folder ALGOCODE. Then, I have command
inputASTM170512 = open("file.txt","r")
I have this present:
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
inputASTM170512 = open("ASTM-170512.txt","r")
IOError: [Errno 2] No such file or directory: 'ASTM-170512.txt'
Why? Do you have any idea?
open() checks only the current working directory and does not traverse your system path looking for the file. Only import works with that mechanism.
You will either need to change your working directory before you open the file, with os.chdir(PATH) or to include the entire path when trying to open it.
When you try to open file with open, for example:
open("ASTM-170512.txt","r")
you will try to open a file in the current directory.
It does not depend on sys.path. The sys.path variable is used when you try to import modules, but not when you open files.
You need to specify the full path to file in the open or change the current directory to the correspondent place (I think that the former is better).

Categories