How to set up a relative path I think? - python

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

Related

How to find the path of a specific file from another folder

Problem definition:
I am running python scripts from Excel using xlwings and I have a file that I am using in these scripts.
I used to have the path specific to my computer (ex--> C:\users\myuser\some_folder\myfile).
now I am sending these scripts to my collogues to use, and I want the path of the file to be automatically configured according to the location of the file on their machines. I tried using os package.
I tried this post and this post and finally tried moving the file in the same location and used os.getcwd() to get the working directory and concatenated with the file name to get the path.
these methods worked fine when I am running the python scripts alone (not from Excel), but When I tried running from Excel it did not work because for some reason when running python scripts from Excel the working directory changes to C:\\ProgramData\\Anaconda3\\
and no longer sees the file. also, these method (according to my understanding) uses the path of the directory from which the file is running.
they are only seeing files in C:\\ProgramData\\Anaconda3\\.
my 1st thought was to try to search for the folder name using this solution but the problem is that I do not know which location the end user will store the folder in.
What I am thinking now is find a way to locate (form this location C:\\ProgramData\\Anaconda3\\ (where python is run from Excel)) the folder which the file is stored at and from there easily grab the file path. after searching the web I did not find a suitable solution for my case.
so, is there a way to do this using os or any other package?
__file__ contains the absolute path to the executed python script without being effected by the current working directory
# example script located at /usr/bin/foo.py
print(__file__)
output:
/usr/bin/foo.py

Selecting images from file explorer to add them in PyCharm directory

When I do image processing in pycharm, I can't add images from my file explorer in the directory. How should I fix it?
I've done what I can as possible but I'm a starter of python. I wasn't able to solve this. So, I know this's not enough by myself.
Since you're using pycharm you must have created a project folder in a directory
That directory is located at C:\Users<user>\PycharmProjects if you have done default installation this will be visible in file explorer and you could use normal copy paste then

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 “../“ :)

JupyterLab - python open() function results in FileNotFoundError

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.

How do I find the exact location of a file in python with pygame?

I need to have the exact directory of an image for a game I'm making in python with pygame. I know what the folder and file are called, but not where my user puts it. Please help, I won't be able to continue my project without you guys :(
The file i'm trying to find is in the same folder as the .py and is a .bmp
If you know the path relative to where your python module is stored in the file system (as you seemed to indicate) you can use the following to calculate the abs path and then build up an appropriate path from there.
import os
print(os.path.abspath(__file__))
print(os.path.split(os.path.abspath(__file__)))
NOTE: Change print for python2.7
Use os.path.join to build up the new path.
Maybe have a look at the os.path documentation page, especially at os.path.abspath.

Categories