write to text file in plugin resources directory

is this possible?

I am able to access .jpg’s using R() and I have attempted various python examples for open(filepath, “w”) but no success.

Hoping someone with more experience in Channel development may be able to point me in the right direction on this.

You’ll need to set this in the info.plist file

<key>PlexPluginCodePolicy</key> <string>Elevated</string>

@dane22 said:
You’ll need to set this in the info.plist file

<key>PlexPluginCodePolicy</key> <string>Elevated</string>

thank you for the quick response. I added the Key and string that you recommended to the info.plist file. (it wasn’t there before) thank you for that.

I am trying the following after making the adjustment to info.plist but still not working for me.

    filepath = '/:/plugins/com.plexapp.plugins.ustvnow/resources/test.txt?t=1482870309'
    fle = open(filepath, "w")
    fle.write('test') 
    fle.close()

any ideas on what I may be doing wrong?

First of all, use io.open

secondly, verify, that file path resolves, by printing it out, cuz not sure about that one!

@dane22 said:
First of all, use io.open

secondly, verify, that file path resolves, by printing it out, cuz not sure about that one!

maybe my file path is the problem. I used the following but wrote it to Log() so I could post in here.

Should I use something else to resolve the path?
R('test.txt')

I would get the path to the bundle dir, and then work from there

Take a peak @ line 36 here:
github.com/dagalufh/WebTools.bundle/blob/master/Contents/Code/consts.py

/T

@dane22
Thank you for your help on this. it ended up being a combination of all three of your answers so I accepted them all.

final code:

versionFile = Core.storage.join_path(Core.app_support_path, Core.config.bundles_dir_name, TITLE + '.bundle', 'Contents', 'Resources', 'test.txt')
fle = io.open(versionFile, "wb")
fle.write('test') 
fle.close()

Glad you got it working, and very glad you provided the final code.

That way, we all learn, which is what this forum is all about :slight_smile:

/Tommy

Here is a shorter path

versionFile = Core.storage.join_path(Core.bundle_path, 'Contents', 'Resources', 'test.txt')
# suggest using with statement, will handle closing for you
with io.open(versionFile, 'wb') as f:
    f.write('test')

Also it’s a good idea to expand relative paths by using Core.storage.abs_path(), YMMV.


On a side note, the Plex Framework does include a Datakit which allows the developer to read and write raw binary files or objects into the channels support directory. More info on the Datakit can be found in PlexPlug-inFramework.pdf, page 78.


Bonus: Another developer asked me about general channel development the other day. I compiled a small list of resources for them and thought you might find it useful too. Refer to Comment_1324842.

@Twoure said:
Here is a shorter path

versionFile = Core.storage.join_path(Core.bundle_path, 'Contents', 'Resources', 'test.txt')
# suggest using with statement, will handle closing for you
with io.open(versionFile, 'wb') as f:
    f.write('test')

Also it’s a good idea to expand relative paths by using Core.storage.abs_path(), YMMV.


On a side note, the Plex Framework does include a Datakit which allows the developer to read and write raw binary files or objects into the channels support directory. More info on the Datakit can be found in PlexPlug-inFramework.pdf, page 78.


Bonus: Another developer asked me about general channel development the other day. I compiled a small list of resources for them and thought you might find it useful too. Refer to Comment_1324842.

thank you for weighing in on this; your reference to the Datakit also works and I did not know about it.

thank you for the additional documentation as well, seems I have a lot of casual reading ahead of me.