Python Resource Paths?

For questions about using Classic.

Post » Wed May 25, 2011 2:19 pm

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?
B
2
S
2
G
1
Posts: 121
Reputation: 1,092

Post » Wed May 25, 2011 4:49 pm

[quote="zeno":75jpkwxo]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?[/quote:75jpkwxo]

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.
[code:75jpkwxo]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)
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Wed May 25, 2011 9:44 pm

Thanks tulamide :)
B
2
S
2
G
1
Posts: 121
Reputation: 1,092


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 9 guests