Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File Opening
#1
I cannot open the file below. Both the .py file and the salesdata.csv are stored in the same directory.

This is the error :
FileNotFoundError: [Errno 2] No such file or directory: 'salesdata.csv'

Any advice ?

# Import pyplot graphing function
import matplotlib.pyplot as plt

# Open the file and read the lines
f = open('salesdata.csv','r')
salesfile = f.readlines()
Reply
#2
check your file name/location
also, is CWD the same in which the files are located?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
How do I check this ?
In my directory both the Python file and the data file are both there ?
Reply
#4
The getcwd function in the os module will tell you what the current working directory (CWD) is (docs here):

>>> import os
>>> os.getcwd()
'/tmp'

It's not really about where your program is, it's more about where you're running it from (i.e. its CWD). If you're still stuck, then showing the CWD and path to the file should help us help you.
Reply
#5
This is the code, and the error message when running it.
runcell(0, 'C:/Users/heetesh.j.bhoola/Desktop/PYTHON FILES/untitled2.py')
Traceback (most recent call last):

File "C:\Users\heetesh.j.bhoola\Desktop\PYTHON FILES\untitled2.py", line 14, in <module>
f = open('salesdata.csv','r')

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


# Import pyplot graphing function
import matplotlib.pyplot as plt

# Open the file and read the lines
f = open('salesdata.csv','r')
salesfile = f.readlines()


import os
os.getcwd()
'/tmp'
Reply
#6
try

import os
import sys

cwd = os.path.dirname(sys.argv[0])
    
with open(f'{cwd}/salesdata.csv','r') as f:
    salesfile = f.readlines()
    print(salesfile)
Reply
#7
Thanks.
I ran the code, get the following error. I'm really lost on this seemingly simple task.

runcell(0, 'C:/Users/heetesh.j.bhoola/Desktop/PYTHON FILES/untitled2.py')
Traceback (most recent call last):

File "C:\Users\heetesh.j.bhoola\Desktop\PYTHON FILES\untitled2.py", line 9, in <module>
cwd = os.path.dirname(sys.argv[0])

NameError: name 'sys' is not defined
Reply
#8
you need to import sys, as shown by @Alex_Erfurt
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
I have also had some issues, try copying your csv file into another directory and then only after that copying your .py file into the same directory.
Reply
#10
Thanks all for your assistance.
This is still not solved, and have tried all suggestions.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Facing Problem while opening a file through command prompt vlearner 4 1,904 Jan-30-2022, 08:10 AM
Last Post: snippsat
  Subprocesses not opening File Select Dialog teut 2 2,403 Feb-22-2021, 08:07 PM
Last Post: teut
  Opening file and outputting its text content example leodavinci1990 1 1,877 Oct-12-2020, 05:33 AM
Last Post: buran
  Test a file for a string without opening it? tester_V 2 2,136 Jun-03-2020, 06:40 PM
Last Post: tester_V
  How to assess elapsed time to execute a .exe file or opening a desktop application? Sudershan 2 2,138 Apr-18-2020, 01:40 PM
Last Post: buran
  Opening a file amitsinbox 6 3,145 Apr-13-2020, 11:54 AM
Last Post: amitsinbox
  Opening CSV file from SFTP server not working cluelessintern 0 2,767 Apr-08-2020, 08:10 PM
Last Post: cluelessintern
  How To Find an Opening and Closing String, Copying Open/Close/Contents to New File davidshq 1 2,029 Mar-03-2020, 04:47 AM
Last Post: davidshq
  Error -3 when opening matlab file in python python_newbie09 0 2,292 Aug-02-2019, 11:51 AM
Last Post: python_newbie09
  python script file not opening in IDLE editor srm 2 4,380 Jun-23-2019, 08:45 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020