#🔒 No such file or directory, Python can't find the obvious csv file
36 messages · Page 1 of 1 (latest)
@visual brook
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.
dtf = open("nydata.csv", "r") # r=read
lines = dtf.readlines()
FileNotFoundError: [Errno 2] No such file or directory: 'nydata.csv'
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"
do i have download ospath with pip @buoyant comet
is that a no? im not good at this sorry🥹 just tryna make it through this one project
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()
?
!with
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.
somethhing else to look at
does the "with" command basically replace the variable
and just open it as a one time thing
kind of
you'd do ```py
with open('seomthing') as file:
lines = file.readlines()
print(*lines)
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.
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.