1

I am doing a 30 Days of Python course on Udemy. In Day 16 part 2 I got stuck with creating edit_data (in my code: edytuj_dane) function. I would like to store some values to a temporary file (NamedTemporaryFile object) using DictWriter and then move the content to a CSV file (dane.csv) that should by updated by each command of the function.

I got confused as there is an error that I can't access the temp file created by NamedTemporaryFile object, whereas checking the path it seems that it is not available at all (also checked if it's hidden). Could you please check and let me know if you have any ideas how to fix the error?

The dane.csv file content is as below:

id,name,email,amount,sent,date

1,2,3,4,5,6

And the executed code is below:

def edycja_danych(edit_id = None, email = None, sent = None, amount = None):
    nazwa_pliku = 'data.csv'
    plik_tymczasowy = NamedTemporaryFile(delete=True, mode ='w') 

    with open(nazwa_pliku, 'r') as csvfile, plik_tymczasowy:
        reader = csv.DictReader(csvfile)
        tytuly = ['id','name','email', 'amount', 'sent', 'date']
        writer = csv.DictWriter(plik_tymczasowy, fieldnames = tytuly)
    
        writer.writeheader() 
        
        for row in reader:
            writer.writerow(row)
            shutil.copyfile(plik_tymczasowy.name, nazwa_pliku)

edycja_danych(email='qwerty@gmail.com', amount = 90, sent = '')

Actually the code execution ends up with following error:

PermissionError: [Errno 13] Permission denied: 'C:\Users\Marek\AppData\Local\Temp\tmpjr622sbn'

Whereas, it should (I think so at least) end up with no error and the file being updated.

Community
  • 1
  • 1
markoo
  • 19
  • 3

0 Answers0