# Deleting a line from a file

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)

• Try OpenWrite and Write to a stream. – David G. Stork Jul 2 '15 at 17:14
• Overwriting your last backup doesn't seem a judicious choice ... – Dr. belisarius Jul 2 '15 at 17:27
• 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. – E.O. Jul 2 '15 at 19:51
• 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... – Albert Retey Jul 3 '15 at 15:12
• @AlbertRetey thank you, that is something I didn't consider! I will definitely do that. – E.O. Jul 3 '15 at 17:46

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" *)

• Thank you! This is exactly what I was looking for :) – E.O. Jul 3 '15 at 8:02
str = OpenWrite[]

Write[str, "my first stuff"]

Write[str, "my second stuff"]

Close[str]

FilePrint[%]


"my first stuff" "my second stuff"

• Thanks for your response! However, this doesn't overwrite the previous content. – E.O. Jul 2 '15 at 17:21