> For the complete documentation index, see [llms.txt](https://stage-precision.gitbook.io/grid/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://stage-precision.gitbook.io/grid/scripting/api-reference/utility-api/files-and-directories.md).

# Files & Directories

### `createDirectory()`

Creates a directory at the specified path.

#### Syntax

```python
sp.util.createDirectory(path)
```

#### Parameters

| Parameter | Type  | Description              |
| --------- | ----- | ------------------------ |
| `path`    | `str` | Directory path to create |

#### Returns

`bool`

Returns `True` when the directory was created successfully, otherwise `False`.

#### Example

```python
sp.util.createDirectory("C:/Folder")
```

### `directoryExists()`

Checks whether a directory exists.

#### Syntax

```python
sp.util.directoryExists(path)
```

#### Parameters

| Parameter | Type  | Description             |
| --------- | ----- | ----------------------- |
| `path`    | `str` | Directory path to check |

#### Returns

`bool`

#### Example

```python
exists = sp.util.directoryExists("C:/Folder")

print(exists)
```

### `fileExists()`

Checks whether a file exists.

#### Syntax

```python
sp.util.fileExists(path)
```

#### Parameters

| Parameter | Type  | Description        |
| --------- | ----- | ------------------ |
| `path`    | `str` | File path to check |

#### Returns

`bool`

#### Example

```python
exists = sp.util.fileExists("C:/image.png")

print(exists)
```

### `readDirectory()`

Returns the files contained in a directory.

#### Syntax

```python
sp.util.readDirectory(path)
```

#### Parameters

| Parameter | Type  | Description       |
| --------- | ----- | ----------------- |
| `path`    | `str` | Directory to read |

#### Returns

An array containing the file names found in the directory.

Subdirectories are not included.

#### Example

```python
files = sp.util.readDirectory("C:/Folder")

print(files)
```

Example result:

```python
['image1.png', 'image2.png']
```

{% hint style="info" %}
`readDirectory()` returns files only. Use `readFolderInDirectory()` to retrieve subdirectories.
{% endhint %}

### `readFolderInDirectory()`

Returns the folders contained in a directory.

#### Syntax

```python
sp.util.readFolderInDirectory(path)
```

#### Parameters

| Parameter | Type  | Description       |
| --------- | ----- | ----------------- |
| `path`    | `str` | Directory to read |

#### Returns

An array containing the folder names found in the directory.

#### Example

```python
folders = sp.util.readFolderInDirectory("C:/Folder/")

print(folders)
```

### `readFile()`

Reads the content of a file.

#### Syntax

```python
sp.util.readFile(path)
```

#### Parameters

| Parameter | Type  | Description  |
| --------- | ----- | ------------ |
| `path`    | `str` | File to read |

#### Returns

The content of the file.

#### Example

```python
content = sp.util.readFile(
    "C:/Folder/TextDocument.txt"
)

print(content)
```

### `renameFile()`

Renames a file.

#### Syntax

```python
sp.util.renameFile(path, newName)
```

#### Parameters

| Parameter | Type  | Description              |
| --------- | ----- | ------------------------ |
| `path`    | `str` | Current path of the file |
| `newName` | `str` | New file name            |

#### Returns

`bool`

Returns `True` when the file was renamed successfully, otherwise `False`.

#### Example

```python
sp.util.renameFile(
    "C:/Folder/image.png",
    "NewName.png"
)
```

{% hint style="info" %}
`newName` specifies the new file name, not a complete destination path.
{% endhint %}

### `writeFile()`

Writes content to a file.

#### Syntax

```python
sp.util.writeFile(path, content)
```

#### Parameters

| Parameter | Type  | Description                 |
| --------- | ----- | --------------------------- |
| `path`    | `str` | File path to write          |
| `content` | `str` | Content written to the file |

#### Returns

`bool`

Returns `True` when the file was written successfully, otherwise `False`.

#### Example

```python
sp.util.writeFile(
    "C:/Folder/Text.txt",
    "Content"
)
```

### Quick Reference

| API                                   | Description                              |
| ------------------------------------- | ---------------------------------------- |
| `sp.util.createDirectory(path)`       | Creates a directory                      |
| `sp.util.directoryExists(path)`       | Checks whether a directory exists        |
| `sp.util.fileExists(path)`            | Checks whether a file exists             |
| `sp.util.readDirectory(path)`         | Returns files contained in a directory   |
| `sp.util.readFolderInDirectory(path)` | Returns folders contained in a directory |
| `sp.util.readFile(path)`              | Reads the content of a file              |
| `sp.util.renameFile(path, newName)`   | Renames a file                           |
| `sp.util.writeFile(path, content)`    | Writes content to a file                 |
