Home > database >  Permission denied: Writing to CSV from Python
Permission denied: Writing to CSV from Python

Time:06-12

Im receiving the below error when attempting to write to a CSV file. I checked my file and folder settings and global availability is set. How do I fix this? Code is below as well.

I have tried with both open and closed files.

File "c:\Users\toril\OneDrive\Documents\Pokemon AI\base 1 test.py", line 19, in <module>
    with open ('pokemontest1.csv', 'w', newline='') as csv_file:
PermissionError: [Errno 13] Permission denied: 'pokemontest1.csv'


    from importlib.resources import Resource
from string import capwords
from unicodedata import name
from pokemontcgsdk import RestClient
from pokemontcgsdk import Card
from pokemontcgsdk import Set
from pokemontcgsdk import Type
from pokemontcgsdk import Supertype
from pokemontcgsdk import Subtype
from pokemontcgsdk import Rarity
from pokemontcgsdk import querybuilder
from pokemontcgsdk import RestClient
import csv

RestClient.configure('xxxxxxxxxxxx')

fields = ['Data']

with open ('pokemontest1.csv', 'w', newline='') as csv_file:
    Cards = Card.where(q='set.name:generations supertype:pokemon')
for card in Cards:
    rows = [card.name, card.types, card.supertype, card.subtypes, card.number, card.rarity, card.nationalPokedexNumbers, card.id, card.set.name, card.set.series]
    
    csvwriter = csv.writer(csv_file)
    csvwriter.writerow(fields)
    csvwriter.writerows(rows)

CodePudding user response:

The reason it wasnt working is because as soon as you exit the with statement the file is closed and no longer writeable.

For example:

with open(filename) as file:
    contents = file.read()

contents = file.read()  # <-- will throw error

What your code was doing was equivalent to this:

csv_file = open("pokemontest2.csv", "w", newline='')
Cards = Card.where(q='set.name:generations supertype:pokemon')
csv_file.close()

for card in Cards:
    rows = [card.name, card.types, card.supertype, card.subtypes, card.number, card.rarity, card.nationalPokedexNumbers, card.id, card.set.name, card.set.series]
    csvwriter = csv.writer(csv_file)  # csv_file is no longer open
    csvwriter.writerow(fields)
    csvwriter.writerows(rows)

By moving the with statement down, now all of the logic that deals with the csv_file is inside the with block.

Cards = Card.where(q='set.name:generations supertype:pokemon')
for card in Cards:
    rows = [card.name, card.types, card.supertype, card.subtypes, card.number, card.rarity, card.nationalPokedexNumbers, card.id, card.set.name, card.set.series]
    
with open ('pokemontest1.csv', 'w', newline='') as csv_file:
    csvwriter = csv.writer(csv_file)  # the file is still open
    csvwriter.writerow(fields)
    csvwriter.writerows(rows)

CodePudding user response:

Three things fixed it:

  1. Moving the file outside of VS code.
  2. Closing the file, changing the name and allowing it to rewrite a new one.
  3. Moving the With Open statement down in my code.
  • Related