Skip to content

Getting resources folder location

VB PROGER edited this page Jun 6, 2023 · 3 revisions

Resource folder (no virtual mode)

The resource folder is given to program when it is executed. For example, this is our code:

#!/usr/bin/env python3
from sys import argv # Load arguments
from os.path import join # Import path joining function

args = argv[1:] # Argument 0 = file name, Argument 1 = resources folder
resources_folder = ''.join(args) # Concatenate list to string
print(args) # Print the resources folder

with open(join(resources_folder, 'example.txt'), 'r+') as f:
    print(f.read().strip()) # Print contents of example.txt located in resources folder

After executing:

['/tmp/Resources/VBPROGER.resources-loading-example']
This is an example of using resources

Get the example here.

Virtual mode

In the virtual mode (--virtual in arguments), ProgramPack cd's to the temporary folder, so you can just use relative paths (../data/nice/hello.txt) as long as you don't use absolute paths (/home/username/data/nice/hello.txt) because they break the system. Here is an example of using virtual mode:

#!/usr/bin/env python3
with open('example.txt', 'r+') as f: # Open file "`example.txt`"
    print(f.read().strip()) # Print contents of example.txt located in resources folder

After executing:

This is an example of using resources

Clone this wiki locally