OpenWrite

OpenWrite["file"]
opens a file to write output to it, and returns an OutputStream object.

OpenWrite[]
opens a new file in the default area for temporary files on your computer system.

Details and OptionsDetails and Options

  • OpenWrite deletes any existing contents in a file, and prepares to write output starting at the beginning of the file.
  • The following options can be given:
  • BinaryFormatFalsewhether to use binary format for the file
    CharacterEncodingAutomaticwhat raw character encoding to use
    FormatTypeInputFormdefault format for printing expressions
    NumberMarks$NumberMarkswhen to use ` marks in approximate numbers
    PageWidth78number of character widths per line
    TotalWidthInfinitymaximum number of character widths for a single expression
    MethodAutomaticstream method to use
  • On computer systems that support pipes, OpenWrite["!command"] runs the external program specified by command, and opens a pipe to send input to it.
  • If OpenWrite does not succeed in opening a particular file or pipe, it generates a message and returns $Failed.
  • OpenWrite resolves file names according to the procedure described in "Files and Streams".
  • OpenWrite returns OutputStream["name",n], where name is the full name of a file or command, and n is a serial number that is unique across all streams opened in the current Wolfram System session.
  • SetOptions can be used to change the properties of an output stream after it is already open.
  • Functions like Put and Write automatically open the files or pipes they need, if they are not already open.
  • With the default setting BinaryFormat->False, the setting for CharacterEncoding is taken to be "ASCII".
  • OpenWrite[] always creates a new file that does not already exist, in the directory given by $TemporaryDirectory.
  • With the Method option, the stream is opened using the given input stream method. This overrides the default way that OpenWrite resolves file names.
  • OpenWrite[File["file"]] is also supported.

ExamplesExamplesopen allclose all

Basic Examples  (1)Basic Examples  (1)

Create and open a new stream for writing:

In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
Out[2]=

Write an expression to the stream:

In[3]:=
Click for copyable input

Close the stream:

In[4]:=
Click for copyable input
Out[4]=

Print the resulting file:

In[5]:=
Click for copyable input
Introduced in 1988
(1.0)
| Updated in 2016
(11.0)