Python Forum
io.UnsupportedOperation: not readable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
io.UnsupportedOperation: not readable
#1
I've already imported csv, so I don't understand why it's complaining about line 20 in the following code:

#!/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, "w", 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()
Error:
Traceback (most recent call last): File "I:\Python\Python36-32\SamsPrograms\MovieListCSV.py", line 72, in <module> main() File "I:\Python\Python36-32\SamsPrograms\MovieListCSV.py", line 56, in main movies = readMovies() File "I:\Python\Python36-32\SamsPrograms\MovieListCSV.py", line 20, in readMovies for row in reader: io.UnsupportedOperation: not readable >>>
Reply
#2
The opened file is in write mode not 'r' for reading.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  io.UnsupportedOperation: not readable navidmo 1 1,427 Oct-31-2019, 11:04 PM
Last Post: ichabod801
  Display output in readable format and save hnkrish 1 1,006 Jul-19-2019, 09:29 AM
Last Post: Larz60+
  How to convert Python crawled Bing web page content to human readable? dalaludidu 4 1,466 Sep-02-2018, 04:15 PM
Last Post: dalaludidu
  Time Difference in Epoch Microseconds then convert to human readable firesh 4 6,901 Feb-27-2018, 09:08 AM
Last Post: firesh
  which code is more readable? Skaperen 5 3,117 Nov-14-2016, 02:37 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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