Debian Codegraph @

docs/source/examples/fileCreate.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 import nixio
4 import datetime as dt
5
6 file_name = 'file_create_example.nix'
7
8 # create a new file overwriting any existing content
9 nixfile = nixio.File.open(file_name, nixio.FileMode.Overwrite)
10 print("Nix file: %s\n\tformat: %s\n\tversion: %s\n\tcreated at: %s" % (file_name, nixfile.format, nixfile.version,
11 str(dt.datetime.fromtimestamp(nixfile.created_at))))
12 nixfile.close()
13
14 # re-open file for read-only access
15 nixfile = nixio.File.open(file_name, nixio.FileMode.ReadOnly)
16
17 # this command will fail putting out an error
18 try:
19 nixfile.create_block("test block", "test")
20 except ValueError:
21 print("Error caught: cannot create a new group in nix.FileMode.ReadOnly mode")
22
23 nixfile.close()
24
25 # re-open for read-write access, creating new entities will work
26 nixfile = nixio.File.open(file_name, nixio.FileMode.ReadWrite)
27 nixfile.create_block("test block", "test")
28 nixfile.close()
29
30 # the following file open command will fail
31 nonexistingfilename = "nonexistingfile.nix"
32
33 try:
34 nixfile = nixio.File.open(nonexistingfilename, nixio.FileMode.ReadOnly)
35 except RuntimeError as e:
36 print("Error caught: %s" % str(e))
37 print("One cannot open a file, that is not existing and forbidding the library to write it.")
38
39 nixfile = nixio.File.open(file_name, mode=nixio.FileMode.Overwrite, compression=nixio.Compression.DeflateNormal)
40 nixfile.close()