8000 Micropython file truncate doesn't exist · Issue #4775 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

Micropython file truncate doesn't exist #4775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
morrison93 opened this issue May 10, 2019 · 6 comments
Open

Micropython file truncate doesn't exist #4775

morrison93 opened this issue May 10, 2019 · 6 comments

Comments

@morrison93
Copy link

Is there any alternative in MicroPython to truncate a file at the current seek position? I don't want to just empty the file, I want to truncate it at a position.

The truncate function doesn't exist ( https://docs.micropython.org/en/latest/esp8266/tutorial/filesystem.html?highlight=open#creating-and-reading-files https://docs.micropython.org/en/latest/library/builtins.html?highlight=open#open )

Any alternatives?

I thought about copying the whole contents to a new file, but I wanted to avoid that since I'm limited to memory and storage(working on a LoPy4)

@servolan
Copy link
servolan commented Feb 4, 2020

Hello,
Did-you find a solution for this problem. It does interest me because I have the same problem with a Lopy4

@dane-if3
Copy link

I would use this if it was available

@servolan
Copy link

I finally defined my own truncate function:

def Truncate(self, Num):
with open("controle.txt","r") as FText: # controle.txt is the file to truncate
lignes = FText.readlines() # Read the content of the file
with open("controle.txt","w") as FText: # Create a new empty file controle.txt
for i in range(0,Num):
try:
FText.write(lignes[i]) # Paste one line by one line until Num
except:
pass

@DavidEGrayson
Copy link
Contributor
DavidEGrayson commented Apr 10, 2023

I'd appreciate having a truncate function too. I'm trying to use the FAT file system for real-time data logging. I've noticed that it's best to write a bunch of blank data beforehand, because otherwise when you are logging your data, the file has to expand, and that can be slow (e.g. 70 ms on the RP2040). When I'm done with data logging, I'd like to truncate the file so it doesn't have blank data at the end.

log = open("log", "w")
blank_kb = "\n" * 1024
for i in range(16): log.write(blank_kb)
log.seek(0, 0)
print("\n", file=log)
...
# log.truncate() would be nice here
log.close()

@dpgeorge
Copy link
Member

Both the FatFS and LittleFS drivers provide a file truncate function, so this would be straight forward to implement (and of course make it match CPython).

zyfinity01 pushed a commit to zyfinity01/ENGR301-Data-Recorder-WebApp that referenced this issue Nov 11, 2024
…truncate()

Micropython does not support file truncations. This new implementation instead reads and writes each line into a new file, skipping the last line. See micropython/micropython#4775 for more information
@Auguxst-char
Copy link

I could also really use this for a mpy project that I'm working on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants
0