# Is there a way to stop Export if the file size is too big?

I've got code that's exporting a file, but the file might be so big that it will take up too much space on the cluster I'm using and crash other users' processes. Is there anything like MemoryConstrained, but for disk space? Or any way in general to restrict the disk space Export is allowed to use? I'd like the file to export if it would take less than a certain amount of disk space, but not export if it would take more. Is there any way to do this?

• A possible very rough workaround is TimeConstrained or ByteCount because both are correlated with the output size. – ybeltukov Oct 19 '14 at 15:04
• The only reliable way I can suggest is to write your own exporter, which would export in chunks. In such a case, you will be able to inject such constraint into the logic of your exporter. It might be not as hard as it may sound, since in many cases you can use ExportString to export individual chunks (e.g. this is the case for CSV). – Leonid Shifrin Oct 19 '14 at 16:18
• If[StringLength[ExportString[...,...]] > n, ..., ...] wouldn't work? Or would that use up too much memory (ie, it'd use the same amount of memory as it would diskspace) – barrycarter Oct 19 '14 at 17:04
• @Matt, I think the answer may depend on the details, what format is the file you want to export? – rhermans Oct 19 '14 at 18:32
• @rhermans It's a .m file. barrycarter's solution sounds like it would work, since MemoryConstrained could keep it from crashing the cluster. I'll try it and see if it works. – Matt Oct 20 '14 at 15:25

FileByteCount[]

• checking the space on the filesys is also possible: mathematica.stackexchange.com/questions/16804/… StringJoin[ReadList["!dir c:\ ", Word, RecordLists -> True,WordSeparators -> {" ", ","}][[-1, 3 ;; -3]]] // ToExpression – MatrixBreaker Aug 12 '15 at 6:42
• If you also need to have an approximate filesize, it should be good to call ByteCount on whatever you are exporting, as ybeltukov suggested – MatrixBreaker Aug 12 '15 at 6:56