5
$\begingroup$

In version 10.4 the command CreateFile was introduced:

CreateFile documentation

I am using version 10.3.1 on a Raspberry Pi. I have not found any command which does what CreateFile does. Is there one?

In order to use OpenRead, OpenWrite, ReadList and Close, I have created an empty text file in a Text Editor first. But of course I would like to be able to create the file from inside Mathematica. Should I use something like

Export["file.ext",{}]

if I want to make an empty file?

$\endgroup$
5
  • 3
    $\begingroup$ The predecessor to CreateFile is CreateTemporary, but it was more limited - it only creates a temporary file. OpenWrite and OpenAppend will create a file if it doesn't exist. So will Export["file.ext", "","Text"] $\endgroup$
    – Jason B.
    Commented Aug 12, 2016 at 13:19
  • 2
    $\begingroup$ I dont follow the issue, OpenWrite creates the file if it does not exist. Close[OpenWrite["file"]] creates an empty file. (unless there is something peculiar about the Raspberry Pi os. I suppose) $\endgroup$
    – george2079
    Commented Aug 12, 2016 at 18:35
  • $\begingroup$ @george2079 Why not give it as an answer? It is a perfect answer. $\endgroup$ Commented Aug 13, 2016 at 14:01
  • 1
    $\begingroup$ Thank you for the edit, @Alexey Popkov. The readability is improved. By the way, "inside Mathematica" is correct grammar. Here "inside" is used a preposition in its own right. In "I cleaned the inside of the car", "inside" is used as a noun. In American English "Inside of" is sometimes used as a preposition, but I am using something more like British English. I know that my spaces preceding question marks and exclamation marks are not standard - I have borrowed that convention from French :) I feel awkward when the letters touch those two particular punctuation marks ! $\endgroup$
    – Simon
    Commented Aug 20, 2016 at 4:07
  • 1
    $\begingroup$ @Simon Thanks for the lesson (I have removed the redundant "of") and the explanation. I always wondered where it comes from when seeing the spaces before question marks and exclamation marks. I feel them as "floating" and not connected to the sentence, so it awkward to me :) $\endgroup$ Commented Aug 20, 2016 at 6:14

1 Answer 1

11
$\begingroup$

The new function CreateFile has two main tasks,

  1. CreateFile[] - will create a file in the "default area for temporary files on your computer". Like

CreateFile[]
(* "/var/folders/yy/3fh5ypq11nx5032xgf6xy36w000_6y/T/m000005141861" *)
  1. CreateFile["filename"] will create an empty file with the given name.

Prior to version 10.4, you could get the effect of (1) via CreateTemporary[]. (2) could be achieved by any of the methods in this (nonexhaustive, I'm sure) list:

Close@OpenWrite@"file1";
Close@OpenAppend@"file2";
WriteString["file3", ""];
Export["file4", "", "Text"];
Export["file5", {}, "Text"];
Run["touch file6"];
FileByteCount /@ {"file1", "file2", "file3", "file4", "file5", "file6"}
(* {0, 0, 0, 0, 0, 0} *)
$\endgroup$

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.