Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4785

MicroPython • Re: Attribute error in a code that worked perfectly in the video I learned to code it from.

$
0
0
No error is getting displayed, but the file only gets saved in the raspberry pi pico itself and not in the SD Card. And the file also keeps overwriting on itself
It will. It will keep returning the same name and will overwrite that each time. You are asking for a file name which doesn't already exist on '/sd' which it correctly determines. You then write it to '/' not '/sd'. So, the next time it's called, the file won't be on '/sd' so it returns the same suggestion again.

Because the 'card.new_file()' returns the name of the new file, not its directory name. You need to use -

Code:

myfile = card.new_file(uos.listdir('/sd'))with open ('/sd/' + myfile, "w") as f:

Looking at that 'card.new_file' code I find it hard to believe whoever wrote that is an experienced Python or MicroPython programmer.

It can be simplified as below and could be written even better than this -

Code:

class card:  def new_file(files):    n = 0    while "chitti{}.csv".format(n) in files:      n += 1    return "chitti{}.csv".format(n)
With a bit more effort that can be modified to determine the next higher number file to use even if lower numbered files have been deleted.
It worked . Thanks. Now its getting saved in my sd card, and with different names, like its supposed to.

Statistics: Posted by IRONMANgsk2218 — Wed Jul 17, 2024 11:05 am



Viewing all articles
Browse latest Browse all 4785

Trending Articles