In: Computer Science
In Python: how can I fix the IsADirectoryError: [Errno 21] Is a directory: ???
this is my code:
#importing sqlite and pandas
import sqlite3
import pandas as pd
#goal
print("Welcome! The goal of this assigment is to create a database to find meanings and synonyms for given phrases.")
#Connecting database
conn = sqlite3.connect("keilavaldez.db")
#Creating cursor to remove existing specified tables
cur = conn.cursor()
#creating tables
cur.execute("CREATE TABLE Synsets([SynsetID] INTEGER,[Definition] text)")
cur.execute("CREATE TABLE Phrases([SynsetID] INTEGER,[phrase] text)")
conn.commit()
#opening and reading table named "synsets"
with open("//Users//keilavaldez//Desktop//assigment 10", 'r') as f:
for line in f:
data = line.split('\t')
cur.execute('INSERT INTO synsets (SynsetID, Definition) VALUES (?, ?)', (data[0], data[1].strip()))
#opening and reading table named "phrases"
with open("//Users//keilavaldez//Desktop//assigment 10", 'r') as f:
for line in f:
data = line.split('\t')
cur.execute('INSERT INTO phrases (SynsetID, phrase) VALUES (?, ?)', (data[0], data[1].strip()))
#Asking the user to enter a phrase
#checking if the phrase its in the database
#checking phrases even if they are lower
phrase = str(input("Please enter a phrase: "))
query = 'SELECT * FROM phrases WHERE phrase=' + "'"+ phrase.lower() + "'"
df = pd.read_sql_query(query, conn)
#if phrase is not in database, asking the user to try again
if df.empty:
print("Phrase/word not found. Please try again!")
#returning output if output is in database
#printing how many meanings the phrase has
#printing synonyms, printing unique synonyms
#prinit
else:
result_query = 'SELECT DISTINCT s.SynsetID,Definition FROM phrases p INNER JOIN synsets s ON s.SynsetID=p.SynsetID WHERE phrase=' + "'"+ word.lower() + "'"
result_df = pd.read_sql_query(result_query, conn)
lst = result_df['SynsetID'].values.tolist()
query = 'SELECT DISTINCT phrase,SynsetID FROM phrases WHERE SynsetID IN (SELECT DISTINCT s.SynsetID FROM phrases p INNER JOIN synsets s ON s.SynsetID=p.SynsetID WHERE phrase=' + "'"+ word.lower() + "') AND phrase<>"+ "'"+ word.lower() + "'"
df = pd.read_sql_query(query, conn)
syn = df['phrase'].values.tolist()
print("There are "+ str(len(lst)) +" meanings and "+ str(len(syn)) +" unique synonyms")
j=1
#priniting along with the synset ID
#printing all synonyms
for i in lst:
print("The meaning of this word: ",j)
print(result_df[result_df['SynsetID']==i]["The definition of this word: "].values[0])
print("Synset ID: ")
print(i)
print("Synonyms:")
words = df[df['SynsetID']==i]['phrase'].values.tolist()
for w in words:
if w!=phrase.lower():
print(w)
j=j+1
Hello! :)
You are getting this error because:
//Users//keilavaldez//Desktop//assigment 10
is a directory (a folder) and not a file.
I hope this example can make it clearer for you to understand:
prog.py:
def function(location):
try:
with open(location) as f:
for line in f:
print(line)
except Exception as e:
print(e)
if __name__ == '__main__':
function('file')
function('directory')
Here is the snapshot of a demo run:
The function runs successfully without any exceptions when the location leads to a file but it throws the exception you mentioned when the location leads to a directory.
So, you can overcome the exception by replacing these lines like this:
- with open("//Users//keilavaldez//Desktop//assigment 10", 'r') as f:
+ with open("<insert-valid-file-location-here>", 'r') as f:
Hope this helps! :)