0

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!

Anthony
  • 5
  • 1

1 Answers1

2

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.

gajendragarg
  • 471
  • 1
  • 4
  • 11
  • Still don't work though. I tried your method and now it shows "ParserError: Error tokenizing data. C error: Expected 1 fields in line 6, saw 2" – Anthony Feb 20 '22 at 07:32
  • 1
    @Anthony probably, there is something wrong in csv file. Check this for ParseError: https://stackoverflow.com/questions/18039057/python-pandas-error-tokenizing-data – gajendragarg Feb 20 '22 at 07:35
  • But when I copied the files into the same folder as my ipynb file it worked without any error. And of course the file path is as simple as: df = pd.read_csv("1D788.txt") – Anthony Feb 20 '22 at 09:33
  • @Anthony Well, ParseError occurs only when there are some problems in CSV file. Does the content of "1C728-0AP.txt" same as previous file? – gajendragarg Feb 20 '22 at 09:34
  • 1
    Ok man you are right, I gotta use skiprows=5 to skip the first few rows which are not supposed to be header and it works now, thanks a bunch! – Anthony Feb 20 '22 at 09:46