Python Forum
FileNotFoundError: [Errno 2] No such file or directory: 'movies.csv'
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
FileNotFoundError: [Errno 2] No such file or directory: 'movies.csv'
#1
I tried to run my movie list program:

#!/usr/bin/env python3
#MovieListCSV.py
#This program does pretty much the same thing as MovieList2D.py. The only
#difference about this program is that it calls the writeMovies function
#whenever the user updates the list, so that the data is still saved if this
#program crashes.
import csv

FILENAME = "movies.csv"

def writeMovies(movies):
    with open(FILENAME, "w", newline="") as file:
        writer = csv.writer(file)
        writer.writerows(movies)

def readMovies():
    movies = []
    with open(FILENAME, "r", newline="") as file:
        reader = csv.reader(file)
        for row in reader:
            movies.append(row)
        return movies

def listMovies(movies):
    for i in range(len(movies)):
        movie = movies[i]
        print(str(i+1) + ". " + movie[0] + " (" + movie[1] + ")")
    print()

def addMovie(movies):
    name = input("Name: ")
    year = input("Year: ")
    movie = []
    movie.append(name)
    movie.append(year)
    movies.append(movie)
    writeMovies(movies)
    print(name + " was added.\n")

def deleteMovie(movies):
    index = int(input("Movie Number: "))
    movie = movies.pop(index - 1)
    writeMovies(movies)
    print(movie[0] + " was deleted.\n")

def displayMenu():
    print("COMMAND MENU")
    print("list - List all movies")
    print("add  - Add a movie")
    print("del  - Delete a movie")
    print("exit - Exit the program")
    print()

def main():
    displayMenu()
    movies = readMovies()
    while True:
        command = input("Enter Command: ")
        if command.lower() == "list":
            listMovies(movies)
        elif command.lower() == "add":
            addMovie(movies)
        elif command.lower() == "del":
            deleteMovie(movies)
        elif command.lower() == "exit":
            break
        else:
            print("Invalid command. Please try again.")
    print("Bye!")

if __name__ == "__main__":
    main()
But apparently, storing the movies.csv file into the FILENAME global constant didn't work:

Error:
>>> ================================ RESTART ================================ >>> COMMAND MENU list - List all movies add - Add a movie del - Delete a movie exit - Exit the program Traceback (most recent call last): File "F:\Python\Python36-32\SamsPrograms\MovieListCSV.py", line 72, in <module> main() File "F:\Python\Python36-32\SamsPrograms\MovieListCSV.py", line 56, in main movies = readMovies() File "F:\Python\Python36-32\SamsPrograms\MovieListCSV.py", line 18, in readMovies with open(FILENAME, "r", newline="") as file: FileNotFoundError: [Errno 2] No such file or directory: 'movies.csv' >>>
What's wrong? Why isn't movies.csv being created?
Reply
#2
(Jul-01-2018, 06:20 PM)RedSkeleton007 Wrote: What's wrong? Why isn't movies.csv being created?

in readMovies() function (bad names for functions, by the way, not PEP8 compliant) you open the file in 'r' mode. in read mode if the file does not exists, it is not created.
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
(Jul-01-2018, 06:30 PM)buran Wrote: in readMovies() function, you open the file in 'r' mode. in read mode if the file does not exists, it is not created.

If this line:
FILENAME = "movies.csv"
Does not create the file ahead of time for the readMovies function, what else do I need to do to make sure the movies.csv file gets created?
Reply
#4
you don't want empty file. in normal business case if the file does not exists, you would inform user the file does not exists, then advise user to add some movies first before being able to read from the fileFILENAME = "movies.csv"

and no, this
FILENAME = "movies.csv"
just craete a variable FILENAME, that points to str object 'movies.csv', or in simple terms variable FILENAME has value 'movies.csv' which is string. No I/O operations whatsoever.
I'm afraid that you show some lack of basic knowledge - you should revisit this topic
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
#5
also possible - if file does not exists reading option should not be present at all for the user to choose it
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
#6
The logic is a little off as mention bye @buran.
This is a quick and dirty fix,also your del is function not working for me.
if __name__ == "__main__":
    open(FILENAME, 'a').close()
    main()
Output:
C:\code λ python movie.py COMMAND MENU list - List all movies add - Add a movie del - Delete a movie exit - Exit the program Enter Command: add Name: Seven Year: 1995 Seven was added. Enter Command: list 1. Seven (1995) Enter Command: exit Bye! C:\code λ python movie.py COMMAND MENU list - List all movies add - Add a movie del - Delete a movie exit - Exit the program Enter Command: list 1. Seven (1995) Enter Command: add Name: Foo Year: 2000 Foo was added. Enter Command: list 1. Seven (1995) 2. Foo (2000) Enter Command: Exit Bye!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error (Errno 2), File upload with the Flask framework and a public IP Username_Python1 0 242 Mar-28-2024, 01:46 PM
Last Post: Username_Python1
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 1,560 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,300 Jun-27-2023, 01:17 PM
Last Post: diver999
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 980 Jan-23-2023, 04:56 AM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,111 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  no such file or directory in SFTP saisankalpj 2 1,539 Nov-25-2022, 11:07 AM
Last Post: DeaD_EyE
Photo Making Zip file of a file and Directory Nasir 2 1,015 Oct-07-2022, 02:01 PM
Last Post: Nasir
  Failed to execute child process (No such file or directory) uriel 1 1,650 Sep-15-2022, 03:48 PM
Last Post: Gribouillis
  Need Help: FileNotFoundError:[Errno 2] No such file or directory python202209 5 2,628 Sep-12-2022, 04:50 AM
Last Post: python202209
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,365 May-17-2022, 07:49 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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