JupyterLab - python open() function results in FileNotFoundError - python

I am trying to open an existing file in a subfolder of the current working directory. This is my command:
fyle = open('/SPAdes/default/{}'.format(file), 'r')
The filevariable contains the correct filename, the folder structure is correct (working on macOS), and the file exists.
This command, however, results if this error message:
FileNotFoundError: [Errno 2] No such file or directory: [filename]
Does it have anything to do with the way JupyterLab works? How am I supposed to specify the folder srtucture on Jupyter? I am able to create a new file in the current folder, but I am not able to create one in a subfolder of the current one (results in the same error message).
The folder structure is recognized on the same Jupyter notebook by bash commands, but I am somehow not able to access subfolders using python code.
Any idea as to what is wrong with the way I specified the folder structure?
Thanks a lot in advance.

There shouldn’t be a forward slash in front of SPAdes.
Paths starting with a slash exist high up in file hierarchy. You said this is a sub-directory of your current working directory.

Related

WinError 2: The system cannot find the file specified: (Cannot set explicit path to .exe file)

I am attempting to use the package WhiteboxTools. I have installed it to my python environment and imported it to my Jupyter notebook script. The whitebox documentation suggests explicitly setting the path to the executable file if it is not in the same folder as the script being written with the following:
wbt.set_whitebox_dir('/local/path/to/whitebox/binary/')
Another reason I believe this to be the problem is that I set the path to this file in my first code chunk: wbt.set_whitebox_dir('C:\\Users\\maxduso.stu\\Anaconda3\\envs\\geo\\pkgs\\whitebox_tools-2.2.0-py39hf21820d_2\\Library\\bin\\whitebox_tools.exe'), but the error arises in the next code chunk which calls one of the tools:
wbt.breach_depressions_least_cost(
"C:\\Users\\maxduso.stu\\Desktop\\FCOR_599\\project_work\\data\\tif_folder\\full_pa.tif",
"C:\\Users\\maxduso.stu\\Desktop\\FCOR_599\\project_work\\data\\tif_folder\\sa_breached_dem.tif",
dist = 10, #maximum search distancefor breach paths in cells
max_cost=None,
min_dist=True,
flat_increment=None,
fill=True
)
[WinError 267] The directory name is invalid: 'C:\\Users\\maxduso.stu\\Anaconda3\\envs\\geo\\pkgs\\whitebox_tools-2.2.0-py39hf21820d_2\\Library\\bin\\whitebox_tools.exe'
whitebox_tools is one of two files I attempted, the other being whitebox_gui. These files were suggested to me when I searched for ".exe" in the whitebox tools folder within my environment folder. That said, they are type = Application and so that brings me to the question: why did windows autofill "whitebox_tools.exe" in the file search bar but find an application file? Also, considering I cannot find a file of type .exe, where do I go from here?
Note: the tools also don't run without the path set.
Thanks for reading. I hope the question was clear enough.

How to use "../" in VS CODE to acces a folder outside the current folder

I've been trying to access a folder outside my current folder, however when I Use "../images/image.png" it gives a FileNotFound error. Now running this code in IDLE gives no error and works perfectly. How can i use ../ in Vs code?
Using pygame.display.set_icon(pygame.image.load(f"./images/ui/logo.png")) which gives :- FileNotFoundError: No such file or directory. running it in IDLE 3.9, doesnt give me any error
The first thing to understand is that if the code in vscode involves paths, everything is retrieved for the root directory based on the workspace. Even if you use "." in a deep directory under the workspace, this still represents the workspace directory, instead of the current directory.
So what we need to do is to change our thinking and import the files you want with the workspace as the root directory.
This also means that ".." will not appear in vscode. Because all the files we need are in the workspace, we will not use the parent directory outside the workspace.
For example,
"./images/ui/logo.png"
If you place it like this, you can retrieve the file anywhere in the workspace without complex positioning based on the current file.
You can refer to this issue. It has similar problems with you.
I am assuming that both folders are in the same directory. This means that you should use “./“ instead of “../“ :)

How to set up a relative path I think?

I am new to programing so if you need more detail please let me know.
I am using anaconda prompt for a lab exercise and I had to download data for this lab. It says to save it in the same place as the anaconda3 folder was downloaded so that I could use a reference path to get the data.
The problem I am running into is that when I copy and paste the command given to me which is
df = pd.read_csv('../data/gapminder.tsv', sep='\t')
it gives me the error no such file or directory.
Now I know that I can use an absolute path to get to the file a different way I am just curious where I should save the gapminder file so that this command given to me works.
If you want that path to work, the folder with the python file in it and the folder named "data" must be siblings inside the same parent folder.
some_parent_folder_name?
anacoda_notebooks_or_something?
your_notebook_file_name
data
gapminder.tsv

No such file or directory but file exists?

I'm writing a python script where I need to open a ".txt" folder and analyse the text in there.
I have saved this ".txt" document in the same folder as my Python script.
But, when I go to open the file; file = open("words.txt",'r')
I get the error: No such file or directory: 'words.txt'.
I don't understand why this is happening?
Maby it's because your current working directory is different from the directory your files are stored. Try giving the full path to the file
file = open("<full_path>\words.txt",'r')
Check if there's a typo in the code or in the filename of the file
Make sure the file is really under the current working directory. Sometimes similar filenames or info shown in your IDE cause confusions
Make sure your are editing the correct script. Sometimes people copy and paste a script into different places and immediately forgot which one they are actually editing
Hope it helps.
It's because the directory of the .py file you are working with is not the same as the .txt file's path.
So, you need to mention the path like this:
file = open("C:/User/Desktop/Folder/words.txt", 'r')

How to open a file inside a folder which is in the same folder as a Python program?

Basically I am looking for a simple way to open text file in a folder that is inside the same folder as the program.
My directory structure looks like this:
/programfolder/textfiles/textfile
And I'm trying to use open like this:
text=functionthatgetsfilename()
file=open("textfiles/"+text,"r")
What am I doing wrong? Do I just have a typo somewhere?
You need to know the difference between the Current Directory and the directory your script is in. Your current directory is the directory that you started the application from, in the command line (CMD, SH, etc). You can show that with os.path.normpath(os.curdir).
To solve your problem, you can use
file=open(os.path.join(os.path.dirname(__file__),'holdstextfiles',text),'r')
or
os.chdir(os.path.dirname(__file__))
...
The first solution uses the absolute path to your desired file, which is the same no matter what: it's absolute
The second solution changes the current directory before trying to use the relative path that you're using.

Categories