File/Path Functions#

dir - List Directory Contents#

$dir{[glob pattern]}

Uses a glob pattern to list files in a directory.

import os
import ison

dicData = {
    "result": "$dir{*.ipynb}"
}

dicResult = ison.run.Run(xData=dicData, sImportPath=os.curdir)
print(ison.run.ToString(dicResult))
{
    "result": [
        "ison-basics.ipynb",
        "ison-functions-convert.ipynb",
        "ison-functions-file.ipynb",
        "ison-functions-lambda.ipynb",
        "ison-functions-logic.ipynb",
        "ison-functions-math.ipynb",
        "ison-functions-special.ipynb",
        "ison-functions-string.ipynb",
        "ison-functions-struct.ipynb",
        "ison-lambda-calc.ipynb",
        "ison-lambda.ipynb"
    ]
}

path.* - Path Components#

$path.[sub-command 1].[sub-command 2].[...]{[filepath]}

The path sub-commands are applied from left to right, whereby each sub-command uses the result of the previous sub-command as input. For example, to get the name of the parent of a path, you can write $path.parent.name{[path]}. In the following the various sub-commands that are available are listed.

path.info - Path String Components#

$path.info{[filepath]}

Split path into various parts.

import ison

dicData = {
    "__locals__": {
        "sPath": "C:\\Hello\\World\\to.day.txt"
    },
    "result": "$path.info{$sPath}"
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": {
        "posix": "C:/Hello/World/to.day.txt",
        "suffixes": [
            ".day",
            ".txt"
        ],
        "parents": [
            "C:/Hello/World",
            "C:/Hello",
            "C:/"
        ],
        "name": "to.day.txt",
        "stem": "to.day"
    }
}

path.name - Path String Components#

$path.name{[filepath]}

Obtain the ‘name’ part of a path.

import ison

dicData = {
    "__locals__": {
        "sPath": "C:\\Hello\\World\\to.day.txt"
    },
    "result": "$path.name{$sPath}"
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": "to.day.txt"
}

path.parent - Parent Path#

$path.parent{[filepath]}

Obtain the parent path of the given path.

import ison

dicData = {
    "__locals__": {
        "sPath": "C:\\Hello\\World\\to.day.txt"
    },
    "result 1": "$path.parent{$sPath}",
    "result 2": "$path.parent.parent{$sPath}"
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result 1": "C:/Hello/World",
    "result 2": "C:/Hello"
}

path.parents - Parent Path List#

$path.parents{[filepath]}

Obtain the list of parent paths.

import ison

dicData = {
    "__locals__": {
        "sPath": "C:\\Hello\\World\\to.day.txt"
    },
    "result": "$path.parents{$sPath}",
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": [
        "C:/Hello/World",
        "C:/Hello",
        "C:/"
    ]
}

path.parts - Path Parts#

$path.parts{[filepath]}

Split the path in a list of its parts.

import ison

dicData = {
    "__locals__": {
        "sPath": "C:\\Hello\\World\\to.day.txt"
    },
    "result": "$path.parts{$sPath}",
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": [
        "C:\\",
        "Hello",
        "World",
        "to.day.txt"
    ]
}

path.stem - Path Stem#

$path.stem{[filepath]}

Get the stem (name without suffix) of the last element in the path.

import ison

dicData = {
    "__locals__": {
        "sPath": "C:\\Hello\\World\\to.day.txt"
    },
    "result": "$path.stem{$sPath}",
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": "to.day"
}

path.suffix - Path Suffix#

$path.suffix{[filepath]}

Get the suffix of the last element in the path.

import ison

dicData = {
    "__locals__": {
        "sPath": "C:\\Hello\\World\\to.day.txt"
    },
    "result": "$path.suffix{$sPath}",
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": ".txt"
}

path.suffixes - Path Suffix#

$path.suffixes{[filepath]}

Get the suffixes of the last element in the path.

import ison

dicData = {
    "__locals__": {
        "sPath": "C:\\Hello\\World\\to.day.txt"
    },
    "result": "$path.suffixes{$sPath}",
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": [
        ".day",
        ".txt"
    ]
}

read - Read Text/JSON File#

$read{[filepath]}

Read a text file as string or a JSON file as corresponding structure. Only files with suffix .json, .json5 or .ison are interpreted as JSON files.

import os
import ison

dicData = {
    "result": "$read{demo.txt}"
}

dicResult = ison.run.Run(xData=dicData, sImportPath=os.curdir)
print(ison.run.ToString(dicResult))
{
    "result": "A simple demo text,\nwith a number\nof lines.\n"
}

Here is an example of reading a JSON file.

import os
import ison

dicData = {
    "result": "$read{demo.json}"
}

dicResult = ison.run.Run(xData=dicData, sImportPath=os.curdir)
print(ison.run.ToString(dicResult))
{
    "result": {
        "hello": [
            "world",
            1
        ]
    }
}

write - Write Text/JSON File#

$write{[filepath], [data], [create-path=[true|false]], [json-indent=[int]]}

Write a text or JSON file. The arguments create-path and json-indent are optional. By default no path is created and the json output is not indented. The function automatically creates a JSON file, if the given data is not a string independent of the filename extension.

import os
import ison

dicData = {
    "__locals__": {
        "dicA": {
            "a": 1,
            "b": 2
        }
    },

    "result": "$write{_output/demo_write.json, $dicA, create-path=true, json-indent=4}",

    # Read the output file again. JSON files are automatically converted to objects.
    "read output": "$read{_output/demo_write.json}"
}

# Set the import path for the parser,
# so that the relative paths can be converted to absolute path.
dicResult = ison.run.Run(xData=dicData, sImportPath=os.curdir)
print(ison.run.ToString(dicResult))
{
    "result": "_output/demo_write.json",
    "read output": {
        "a": 1,
        "b": 2
    }
}

Write a text file.

import os
import ison

dicData = {
    "__locals__": {
        "sA": "Hello World"  
    },

    "result": "$write{_output/demo_write.txt, $sA, create-path=true}",

    # Read the output file again. JSON files are automatically converted to objects.
    "read output": "$read{_output/demo_write.txt}"
}

# Set the import path for the parser,
# so that the relative paths can be converted to absolute path.
dicResult = ison.run.Run(xData=dicData, sImportPath=os.curdir)
print(ison.run.ToString(dicResult))
{
    "result": "_output/demo_write.txt",
    "read output": "Hello World"
}