Skip to main content

Modifying files

Open, read, write and remove files

In general python, the function you use to work in files most is open(). It takes two parameters as inputs; filename and mode.

For the mode you have four options:

1. read a file ("r"): This is the default value, it will open and read the file and returns an error if the file does not exist.

2. write to a file ("w"): This opens a file for writing data to. If the file does not exist this will create one with the given filename.

3. create a file ("x"): This will create a new file with the given filename. If a file with that name already exists then this will return an error.

4. append to a file ("a"): This will open a file for appending data. If the file does not exist, it will make a file with the given filename.

Each mode also lets you specify if you would like to handle the file as text ("t") or binary ("b"). (You can also open a file for updating ("+") which lets you read and write).

All of the above can be applied in this example below. Note that in this example we are opening a text file to write to:

file_path = "path/to/text_file.txt"
with open(file_path_, "wt") as file:
# do something with the file

Using a VIKTOR file object in binary mode

One of the features of the VIKTOR platform is the VIKTOR File object. This file like object can be used for files and opens a door to file handling that you can apply.

The important thing to understand is that this method will open and/or read the files in binary mode. As binary mode is consistent across packages, there is no extra encoding or decoding that you may encounter when dealing with non-text files. On that note, using a binary method you can open file formats that may get corrupted by a text mode.

Another advantage of the binary mode is that it allows for efficient stream based processing. This allows for working with larger files without loading the entire contents into memory.

The basic way to open a VIKTOR file object in binary mode is to use open_binary(). You may refer to the example below:

my_file = #path to a csv file as VIKTOR File object

with my_file.open_binary() as fp:
df.read_csv(fp)

If you would like to save a figure like those created by Matplotlib, you can also use open_binary().

fig = Figure()
...
my_svg = File()

with my_svg.open_binary() as fp:
fig.savefig(fp, "svg")
Be mindful of big files

When using the method above, the entire file gets opened meaning large files will use a lot of memory. On these files it is better to use the previous method.

In case you would like to extract data, you may use:

# binary mode
some_bytes = my_file.getvalue_binary() #limited by memory

# text mode (only for text-based data types)
some_strings = my_other_file.getvalue()

In summary, using the VIKTOR file object is the most versatile way to handle your files. The code will only require up to 3 lines, it keeps the code clean and is compatible across platforms reliably.

Using packages

In some cases it is possible to use a package to read the content of a file. An easy example is turning a .csv document into a Pandas dataframe. Dataframes are a commonly used way to store structured data because they are easy to modify and analyse. See the package documentation to see the possibilities.