1
$\begingroup$

I'm writing a Mathematica script that writes output to a file every 10min as backup. However, I'd like it to overwrite the previous content so as not to use to much storage. Is there a function for this? (The only method I can see atm is DelteFile and then Export, but I feel like this is too much work for something this simple)

$\endgroup$
5
  • $\begingroup$ Try OpenWrite and Write to a stream. $\endgroup$ Jul 2, 2015 at 17:14
  • 2
    $\begingroup$ Overwriting your last backup doesn't seem a judicious choice ... $\endgroup$ Jul 2, 2015 at 17:27
  • $\begingroup$ Perhaps backup is a bad choice of words. I'm running a script on a cluster and it may be terminated so I want to periodically output the current state so that I can restart it from there. However I have a storage quota and each update is quite large so want to overwrite the previous update. $\endgroup$
    – E.O.
    Jul 2, 2015 at 19:51
  • $\begingroup$ for that application I would strongly suggest to only delete after writing. If your job terminates/crashes during "backup" all efforts will be lost. So if your quota allows two copies, it would make much more sense to write first and only delete when writing has succesfully finished... $\endgroup$ Jul 3, 2015 at 15:12
  • $\begingroup$ @AlbertRetey thank you, that is something I didn't consider! I will definitely do that. $\endgroup$
    – E.O.
    Jul 3, 2015 at 17:46

2 Answers 2

3
$\begingroup$

You are actually looking for Put. Notice that the syntax is reversed from Write.

str = "myfile.txt"

Put["my first stuff", str]

Put["my second stuff", str]

FilePrint[%]
(* "my second stuff" *)
$\endgroup$
1
  • $\begingroup$ Thank you! This is exactly what I was looking for :) $\endgroup$
    – E.O.
    Jul 3, 2015 at 8:02
0
$\begingroup$
str = OpenWrite[]

Write[str, "my first stuff"]

Write[str, "my second stuff"]

Close[str]

FilePrint[%]

"my first stuff" "my second stuff"

$\endgroup$
1
  • $\begingroup$ Thanks for your response! However, this doesn't overwrite the previous content. $\endgroup$
    – E.O.
    Jul 2, 2015 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.