#🔒 No such file or directory, Python can't find the obvious csv file

36 messages · Page 1 of 1 (latest)

visual brook
#

I need to present a dataset for a school project, trying to make an exponential regression of the data but it won't find the csv file, even though I wrote everything correctly

thorn ridgeBOT
#

@visual brook

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

visual brook
#

dtf = open("nydata.csv", "r") # r=read
lines = dtf.readlines()

#

FileNotFoundError: [Errno 2] No such file or directory: 'nydata.csv'

buoyant comet
#

it's looking for nydata.csv in the current directory, not relative to the script path.

#

You can get to the correct file by using os.path or pathlib.Path and __file__

#
import os.path

nydata_file = os.path.join(os.path.dirname(__file__), "nydata.csv")

# or

from pathlib import Path

nydata_file = Path(__file__).parent / "nydata.csv"
visual brook
#

do i have download ospath with pip @buoyant comet

buoyant comet
#

os.path is stdlib

#

same with pathlib

visual brook
#

is that a no? im not good at this sorry🥹 just tryna make it through this one project

buoyant comet
#

don't use os.path from pip

#

use the example I sent. It works on all systems

visual brook
#

okok, im just tryna make the lines = dtf.readlines() work

#

because u used another variable instead of that dtf

#

do i just remove this line dtf = open("nydata.csv", "r") # r=read

and change lines = dtf.readlines() so it says nydata_file.readlines()

#

?

buoyant comet
#

nydata_file should replace the argument to open

#
dtf = open(mydata_file)
visual brook
#

EUREKA

#

IT WORKED THANK YOU

#

I LOVE YOU

buoyant comet
#

!with

thorn ridgeBOT
#
The `with` keyword

The with keyword triggers a context manager. Context managers automatically set up and take down data connections, or any other kind of object that implements the magic methods __enter__ and __exit__.

with open("test.txt", "r") as file:
    do_things(file)

The above code automatically closes file when the with block exits, so you never have to manually do a file.close(). Most connection types, including file readers and database connections, support this.

For more information, read the official docs, watch Corey Schafer's context manager video, or see PEP 343.

visual brook
#

?

#

let me read that

#

hold on

buoyant comet
#

somethhing else to look at

visual brook
#

and just open it as a one time thing

buoyant comet
#

kind of

#

you'd do ```py
with open('seomthing') as file:
lines = file.readlines()

print(*lines)

visual brook
#

ohhh i think i understand. ill stick with the other one though my brain understands that a little bit better

#

tthank you for replying so fast and so clearly.

thorn ridgeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.