0

I was trying to add some list of words to a CSV file using python file handling, but it was giving a "PermissionError: [Errno 13] Permission denied:" error. The syntax works for a txt file. Although the file was created, no words were added to it. I tried to check the file properties and it seems okay. I don't know what else to do.

Here is the code:

words = ("Almost     every   creative    person  faces   problems    like    procrastination").split()
words2= ("perfectionism self-doubt at   one time another Even   the successful  ones").split()
words3 = ("But  the feeling of  being   utterly alone   on  your    artist’s    journey insidious It").split()
words4 = ("gnaws    at  your    confidence      weakens your    resolve It  causes  talented    writers to").split()

for word in words:
    word_collect= open('word_collected.csv','w+')
for word2 in words2:
    word_collect = open ( 'word_collected.csv' , 'w+' )
for word3 in words3:
    word_collect = open ( 'word_collected.csv' , 'w+' )
for word4 in words4:
    word_collect = open ( 'word_collected.csv' , 'w+' )
    word_collect.write(str(words))
    word_collect.write ( "\n" )
    word_collect.write ( str ( words2 ) )
    word_collect.write("\n")
    word_collect.write ( str ( words3 ) )
    word_collect.write ( "\n" )
    word_collect.write ( str ( words4 ) )
    word_collect.close()

Output:

Traceback (most recent call last):
  File "C:\Users\Joshua\PycharmProjects\HANGERMAN\venv\Lib\hangerman.py", line 8, in <module>
    word_collect = open ( 'word_collected.csv' , 'w+' )
PermissionError: [Errno 13] Permission denied: 'word_collected.csv'
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • 1
    I think, you cannot open the same file multiple times using 'write-mode' under Windows. – Maurice Meyer Nov 26 '20 at 21:56
  • Your code is very confusing. You're opening the file several times in the 3 first for loops but don't do anything with it. Then you write all list into it in a for loop where you're opening and closing it for every iteration. My guess is that you want to open it once before the loops and then write to it, before closing it. – Ted Klein Bergman Nov 26 '20 at 22:01
  • 1
    Do you have the CSV file open with Excel at the same time? – JeffUK Nov 26 '20 at 22:13

1 Answers1

0

I replicate your code and it is working just fine. One possible reason why you have permission error is word_collected.csv exists in your directory but you don't have the right access to write on it.

If you are using console, type ls -l to view the file permission details.

enter image description here

Nikko J.
  • 5,319
  • 1
  • 5
  • 14