Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit file size when writing one?

Tags:

python

file-io

I am using the output streams from the io module and writing to files. I want to be able to detect when I have written 1G of data to a file and then start writing to a second file. I can't seem to figure out how to determine how much data I have written to the file.

Is there something easy built in to io? Or might I have to count the bytes before each write manually?

like image 626
Alex Amato Avatar asked Oct 22 '10 16:10

Alex Amato


People also ask

Is there a limit to text file size?

There are no limits on the size of the string (other than physical memory limits and processor address space limits). Depending on the number of channels and amount of data in the . txt file the maximum string limit is achieved when the . txt file is approximately 1 GB in size.

What is maximum file size exceeded?

When uploading a project file, a Maximum File Size Exceeded error displays and you are not able to submit your project. This happens if your project file is larger than allowed.


1 Answers

if you are using this file for a logging purpose i suggest using the RotatingFileHandler in logging module like this:

import logging
import logging.handlers

file_name = 'test.log'

test_logger = logging.getLogger('Test')
handler = logging.handlers.RotatingFileHandler(file_name, maxBytes=10**9)
test_logger.addHandler(handler)

N.B: you can also use this method even if you don't use it for logging if you like doing hacks :)

like image 131
mouad Avatar answered Sep 28 '22 13:09

mouad