# Basic File Operations

# Getting a Gio.File Instance

// We use GLib.build_filenamev to abstract away path separators like '/' and '\'
let path = GLib.build_filenamev([GLib.get_home_dir(), "testing.txt"]);
// Let's create a Gio.File instance from our path
let file = Gio.File.new_for_path(path);
1
2
3
4

# Creating a file

# Permissions

Permissions are written in octal notation. See this guideopen in new window for more information on Unix permissions;

Earlier Versions1.50+
const PERMISSIONS_MODE = parseInt('0744', 8);const PERMISSIONS_MODE = 0o744;

# Saving Content

if (GLib.mkdir_with_parents(file.get_parent().get_path(), PERMISSIONS_MODE) === 0) {
  let [success, tag] = file.replace_contents(
    text,
    null,
    false,
    Gio.FileCreateFlags.REPLACE_DESTINATION,
    null
  );
}
1
2
3
4
5
6
7
8
9

# Loading A File's Contents

# Load Entire File

const { Gio, GLib } = imports.gi;
let [success, contents] = file.load_contents(null);
log(contents);
1
2
3
Last Updated: 4/11/2021, 5:57:22 AM