0

I am trying to open up data from a CSV file in my Visual Studio terminal and receive:

''''

Traceback (most recent call last):
  File "/home/jubal/ CrashCourse Python Notes/Chapter 16 CC/Downloading Date/csv 
format/highs_lows.py", line 7, in <module>
    with open(filename) as f:
FileNotFoundError: [Errno 2] No such file or directory: 'sitka_weather_2018_full.csv'

''''

and here is the program highs_lows.py, it is saved in the same folder as sitka_weather_2018_full.csv

''''

    import csv

filename = 'sitka_weather_2018_full.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)

    highs = []
    for row in reader:
        highs.append(row[8])

print(highs)

''''

My latop run linux mint 19.2 cinnamon, also I am able to run this program just fine with jupyter notebook but when I try to convert into a python program and run it in the VS code terminal is when this problem occurs. Newbie to programming so any help would be great. Thanks for your time!

jubal1961
  • 31
  • 1
  • 8
  • Are you sure you have the right path ? – SebNik May 03 '20 at 13:43
  • 1
    Use the full path for the filename, ie. ```/home/jubal/ CrashCourse Python Notes/Chapter 16 CC/Downloading Date/csv format//home/jubal/ CrashCourse Python Notes/Chapter 16 CC/Downloading Date/csv format/```. This is the easy answer. The problem is that the program is not running in the directory that the python file lives. I can post you an longer answer using the ```os.join``` to construct file paths if you want a more generic answer. – pink spikyhairman May 03 '20 at 13:52
  • Where are you running it *from*? How are you running it? Right before the with statement add `import os; print(os.getcwd())` - what happens? – wwii May 03 '20 at 13:53
  • 1
    Does this answer your question? [Python open() gives IOError: Errno 2 No such file or directory](https://stackoverflow.com/questions/12201928/python-open-gives-ioerror-errno-2-no-such-file-or-directory) – wwii May 03 '20 at 13:59
  • im running it from my terminal. I believe bash... but im unsure of how to check. im very new to this so your going to have to walk me thru like a child. thanks for your patience – jubal1961 May 03 '20 at 14:39
  • im just copying and pasting with no idea of whats going on so a more in depth answer would be appreciated. – jubal1961 May 03 '20 at 14:41
  • Im using the Anacoda distribution if that helps at all. – jubal1961 May 03 '20 at 14:45
  • Python 3.7.4 (default, Aug 13 2019, 20:35:49) [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> python "/home/jubal/ CrashCourse Python Notes/Chapter 16 CC/Downloading Date/csv format/highs_lows.py" File "", line 1 python "/home/jubal/ CrashCourse Python Notes/Chapter 16 CC/Downloading Date/csv format/highs_lows.py" ^ SyntaxError: invalid syntax – jubal1961 May 03 '20 at 14:51

2 Answers2

0

I'm guessing you are running the script in a terminal that isn't in the same subfolder as the script and the csv.

To verify this you can just cd into the subdirectory and run the script there.

To be able to call the script from any directory you could provide absolute paths instead of the relative paths in your program.

You can get the absolute path like this:

import os
CURRENT_DIR = os.path.dirname(__file__)
file_path = os.path.join(CURRENT_DIR, 'sitka_weather_2018_full.csv')

with open(file_path) as f:
   reader = csv.reader(f)
   ...

Edit __file__ explanation:

__file__ is a special Python variable, marked by the double underscores on each side, also called "dunder" variables. The __file__ variable is defined by Python for each module it imports or executes and contains the path that points to current module.

So if you execute your script with python highs_lows.py, the variable is set to

"/home/jubal/ CrashCourse Python Notes/Chapter 16 CC/Downloading Date/csv 
format/highs_lows.py"

Since, in this case you want to know the path of the csv-file in the same directory we cut the path, to exclude the name of your script and only hold the path to the directory:

CURRENT_DIR = os.path.dirname(__file__)

In the last step we need to concatenate the path to the directory with the path to the csv-file to get the absolute path, e.g:

"/home/jubal/ CrashCourse Python Notes/Chapter 16 CC/Downloading Date/csv 
    format/sitka_weather_2018_full.csv"

instead of the relative path in your first example "./sitka_weather_2018_full.csv"

Here is another post that goes more in depth on the concept:

  • what does __file__ mean? – jubal1961 May 03 '20 at 14:37
  • im so unlcear of what this means, you are going to to have to walk me thru this like a child. Im just getting used to programming – jubal1961 May 03 '20 at 14:38
  • Python 3.7.4 (default, Aug 13 2019, 20:35:49) [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> python "/home/jubal/ CrashCourse Python Notes/Chapter 16 CC/Downloading Date/csv format/highs_lows.py" File "", line 1 python "/home/jubal/ CrashCourse Python Notes/Chapter 16 CC/Downloading Date/csv format/highs_lows.py" ^ SyntaxError: invalid syntax – jubal1961 May 03 '20 at 14:51
  • I've updated the answer with an explanation to the `__file__` variable. Could you update your question with the code, that throws the error you posted? –  May 03 '20 at 15:02
0

This is how I interpreted the answer to be from the previous posts.

import os
import csv


CURRENT_DIR = os.path.dirname(__file__)
file_path = os.path.join(CURRENT_DIR, 'sitka_weather_2018_full.csv')

with open(file_path) as f:
    reader = csv.reader(f)
    header_row = next(reader)

    highs = []
    for row in reader:
        highs.append(row[8])


    print(highs)

And Everything works!

jubal1961
  • 31
  • 1
  • 8
  • Ah, I see. First off, I made a typo in my answer, you can just substitute filename with file_path where you open the file. You can delete the filename variable. Also you need to import the csv package with `import csv` to use the csv.reader. –  May 03 '20 at 15:47
  • That did it! Thanks very much, though I'm still unable to understand exactly how it worked. Do I have to you that same method for every CSV file? – jubal1961 May 03 '20 at 15:55