Python Resource Paths?

This forum is currently in read-only mode.
  • Hi, i have been testing the python examples to load videos using python by R0J0hound and these work great. There is a problem however which is if i try to change it to use the app path i get errors.

    This works -

    flash.LoadMovie(0, r'C:\Tooltip.swf')

    These won't -

    flash.LoadMovie(0, r'.\Tooltip.swf')

    flash.LoadMovie(0, r'System.AppPath + Tooltip.swf')

    I tried various other things but nothing works but the fixed path so i am wondering what is the correct way to make it load a file from the same folder as the app instead?

  • Hi, i have been testing the python examples to load videos using python by R0J0hound and these work great. There is a problem however which is if i try to change it to use the app path i get errors.

    This works -

    flash.LoadMovie(0, r'C:\Tooltip.swf')

    These won't -

    flash.LoadMovie(0, r'.\Tooltip.swf')

    flash.LoadMovie(0, r'System.AppPath + Tooltip.swf')

    I tried various other things but nothing works but the fixed path so i am wondering what is the correct way to make it load a file from the same folder as the app instead?

    You can't use variables inside string literals (r'...'). That's the reason for the last one not working. The second but last is not working, because you need to pass an absolute path, but .\Tooltip.swf is a relative path.

    r indicates that the string literal should be used untouched, not interpreting escape sequences, which start with a backslash. When working with variables, you can't add r as a prefix. There are three ways to pass a working path as a variable (although the first two are not recommended):

    1) Replace all backslashes by slashes.

    my_path = 'C:\folder\data.ext'
    my_path.replace("\", "/") # Converts to 'C:/folder/data.ext'
    flash.LoadMovie(0, my_path)[/code:75jpkwxo]
    
    2) Double all backslashes.
    [code:75jpkwxo]my_path = 'C:\folder\data.ext'
    my_path.replace("\", r"\\") # Converts to 'C:\\folder\\data.ext'
    flash.LoadMovie(0, my_path)[/code:75jpkwxo]
    
    3) The usual way to create and maintain working file paths is using the module os.
    [code:75jpkwxo]import os
    my_path = os.path.join(os.getcwd(), "data.ext") # Creates an absolute path adding data.ext to the current working directory
    flash.LoadMovie(0, my_path)[/code:75jpkwxo]
    os.getcwd() is the Python equivalent to System.AppPath, if your cap is stored in C:\Scirra\myProject then os.path.join(os.getcwd(), "data.ext") returns C:\Scirra\myProject\data.ext (well, the string would read "c:\\scirra\\myproject\\data.ext", but that's how Python works with strings and paths)
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks tulamide

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)