Home > OS >  What is the correct read_csv File Path after os.chdir()?
What is the correct read_csv File Path after os.chdir()?

Time:02-21

Good day. My current ipynb file is located here:

C:\Users\Adams\Desktop\ml-projects\TH-batch

And this is the location of my working text files, where "ID788" is my working text file, "New Folder" and "AZ2080975" are folders:

C:\Users\Adams\Desktop\ml-projects\TH-batch\New Folder\AZ2080975\1D788.txt

While I have changed the working directory to this:

os.chdir(r"C:\Users\Adams\Desktop\ml-projects\TH-batch\New Folder")

As I tried to use read_csv like this:

df = pd.read_csv("../AZ2080975/1D788.txt")

It gives me error like below:

[Errno 2] No such file or directory: '../AZ2080975/1D788.txt'

Could anyone please help me with this? I have tried to increase or decrease the file path in my read_csv but nothing work. Thank you!

CodePudding user response:

Your CWD is "New Folder". You can use this:

df = pd.read_csv("./AZ2080975/1D788.txt")

OR

df = pd.read_csv("AZ2080975/1D788.txt")

to read this file. You were using two dots (..) in your read_csv function so code was look for this file in "TH-batch" folder.

  • Related