write sth. upfront
When reading dataset with pd.read_csv, I have 2 questions?1 is: whether to write relative path or absolute path.2 is: how to write relative path, absolute path. This article is to solve the above two problems. If the script is just on your own computer, it doesn't matter, but if someone else also wants to use your script, I think the relative path is still better, the data set and the script copied to others, if the environment is fine, then the path can be run directly without modification, if you use the absolute path, someone else to get it after you still have to modify the path.
Issues arising
Error, the file was not found in this path, the path was written incorrectly.
sort
Usually the dataset is in a folder with your script. I'm using an absolute path.
Step 1 Print the path where the script is located
import os () print(())
Step 2
Add the path to your dataset
train = pd.read_csv('F:\\pythonProject3\\data\\data\\')
Here are the files for my script and dataset.
Step 3 Test it out.
print(train)
Reading a dataset with a relative path
prerequisitesThe dataset is not under the same file as the script, but in the same upper level folder. This is the case below.
Read file method
train = pd.read_csv('..\\')
figure“..”
It means that it is the absolute path of the previous folder of the folder you are currently in. That's the path to data in the image below
F:\pythonProject3\data\data
If you don't understand it, you can try it yourself.
import os path1=('.') # Indicates the absolute path to the folder you are currently in. print("path1@@@@@",path1) path2=('..') ## Indicates the absolute path to the folder one level up from the folder you are currently in print("path2@@@@@",path2)
Complete Code
import pandas as pd import numpy as np import os () # F:\\pythonProject3\\data\\data\\ # dataset_path = '..' train = pd.read_csv('..\\') path1=('.') print("path1@@@@@",path1) path2=('..') print("path2@@@@@",path2) print(train)
consultation
https:///article/
to this article on the pd.read_csv read file path problems to this article, more related pd.read_csv read file path content please search my previous posts or continue to browse the following related articles I hope you will support me in the future more!