Detect file size and delete

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
guyd
Posts: 74
Joined: Fri Jul 20, 2018 6:08 am

Detect file size and delete

Post by guyd » Sat Sep 08, 2018 6:04 pm

Hi,
On an ESP32 I execute Micropython Code. Since wifi and MQTT error can occur from time to time- I keep an error log saved locally.

I wish to know how can I detect file's size, and delete it if it greater that a certain size ( it will be check from time to time ),

Guy

User avatar
Roberthh
Posts: 1007
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Detect file size and delete

Post by Roberthh » Sat Sep 08, 2018 6:27 pm

The uos module provides the required functions.
uos.stat(filename)[6] returns the file size.
uos.remove(filename) deletes a file
For further Information, consult the documentation: http://docs.micropython.org/en/latest/p ... y/uos.html

guyd
Posts: 74
Joined: Fri Jul 20, 2018 6:08 am

Re: Detect file size and delete

Post by guyd » Sat Sep 08, 2018 6:31 pm

thank you :)

guyd
Posts: 74
Joined: Fri Jul 20, 2018 6:08 am

Re: Detect file size and delete

Post by guyd » Sun Sep 09, 2018 11:37 am

Roberthh wrote:
Sat Sep 08, 2018 6:27 pm
The uos module provides the required functions.
uos.stat(filename)[6] returns the file size.
uos.remove(filename) deletes a file
For further Information, consult the documentation: http://docs.micropython.org/en/latest/p ... y/uos.html
looking at the docs referred - there is no explanation for result vector of uos.stat(filename).

User avatar
Roberthh
Posts: 1007
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Detect file size and delete

Post by Roberthh » Sun Sep 09, 2018 1:41 pm

These are similar to the standard os.stat() of Python. https://docs.python.org/3/library/os.html#os.stat
The symbolic names are not supported, and most fields are not supported by the file system. The three time stamps are all the same. So actually, if you code res=uos.stat(filename), you get

res[0] is the file type (directory or plain file), directory if (res[0] & 0x4000) != 0
res[6] file size
res[8] modification time

guyd
Posts: 74
Joined: Fri Jul 20, 2018 6:08 am

Re: Detect file size and delete

Post by guyd » Sun Sep 09, 2018 4:07 pm

Is there a creation date as well ?

User avatar
Roberthh
Posts: 1007
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Detect file size and delete

Post by Roberthh » Sun Sep 09, 2018 4:39 pm

No. As said, all three time stamps are the same. The file system does not provide more information. And, of course, these time stamps are only reasonable if the time was properly set.
Edit res[7], res[8] and res[9] are these three time stamps.

Post Reply