Session.virtualfile_out(kind='dataset', fname=None)[source]

Create a virtual file or an actual file for storing output data.

If fname is not given, a virtual file will be created to store the output data into a GMT data container and the function yields the name of the virtual file. Otherwise, the output data will be written into the specified file and the function simply yields the actual file name.

  • kind (Literal['dataset', 'grid'], default: 'dataset') – The data kind of the virtual file to create. Valid values are "dataset" and "grid". Ignored if fname is specified.

  • fname (str | None, default: None) – The name of the actual file to write the output data. No virtual file will be created.


vfile (str) – Name of the virtual file or the actual file.


>>> from pathlib import Path
>>> from pygmt.clib import Session
>>> from pygmt.datatypes import _GMT_DATASET
>>> from pygmt.helpers import GMTTempFile
>>> with GMTTempFile(suffix=".txt") as tmpfile:
...     with Path("w") as fp:
...         print("1.0 2.0 3.0 TEXT", file=fp)
...     # Create a virtual file for storing the output table.
...     with Session() as lib:
...         with lib.virtualfile_out(kind="dataset") as vouttbl:
...             lib.call_module("read", [, vouttbl, "-Td"])
...             ds = lib.read_virtualfile(vouttbl, kind="dataset")
...             assert isinstance(ds.contents, _GMT_DATASET)
...     # Write data to an actual file without creating a virtual file.
...     with Session() as lib:
...         with lib.virtualfile_out( as vouttbl:
...             assert vouttbl ==
...             lib.call_module("read", [, vouttbl, "-Td"])
...         line = Path(vouttbl).read_text()
...         assert line == "1\t2\t3\tTEXT\n"