×

Discussion Board

Results 1 to 12 of 12
  1. #1
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    56

    using a configuration file

    hey

    i want my script to write/read configuration file.
    how should i save it ? as in location.
    so it will know if its in memory card or on the phone
    and also after i make my scripts to sis so if a user install to storage card or phone config will be created in appropriate location.

    thx

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    4

    Re: using a configuration file

    Read this, ...
    http://discussion.forum.nokia.com/fo...d.php?t=127983

    you can save your configuration in a file and read the file after that...

  3. #3
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    56

    Re: using a configuration file

    that doesnt answer my question.
    I want to know how my script can know to location to save the configuration file.
    as if to somewhere in the storage card if script is in storage card or to phone memory if script is in phone memory.

  4. #4
    Nokia Developer Moderator
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: using a configuration file

    Quote Originally Posted by roidayan View Post
    that doesnt answer my question.
    I want to know how my script can know to location to save the configuration file.
    as if to somewhere in the storage card if script is in storage card or to phone memory if script is in phone memory.
    Are you talking about a configuration file that you create? Because if it's so, you just specify where to create/save the file in your script. If you're talking about a pre-existing file, I don't know.

  5. #5
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    56

    Re: using a configuration file

    yes i create.
    my question is how i check where to create.
    i want the script to check itself if its on the memory card or the phone memory so based this it will use diff location

  6. #6
    Regular Contributor
    Join Date
    Mar 2003
    Location
    UK
    Posts
    125

    Re: using a configuration file

    Quote Originally Posted by roidayan View Post
    i want my script to write/read configuration file.
    how should i save it ? as in location.
    so it will know if its in memory card or on the phone
    and also after i make my scripts to sis so if a user install to storage card or phone config will be created in appropriate location.
    Best place is probably CWD, which is the directory which the active SIS is installed to. Something like this...

    Code:
    # Example config data
    CONFIG = 'foo';
    
    # Save it
    f = open('config', 'w')
    f.write(CONFIG)
    f.close()
    
    # Load it
    f = open('config', 'r')
    CONFIG = f.read()
    f.close()

  7. #7
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    56

    Re: using a configuration file

    ha ok thx.
    if i run the script from python shell before making it sis
    it will create/read the config from the same dir as the script file ?

  8. #8
    Regular Contributor
    Join Date
    Mar 2003
    Location
    UK
    Posts
    125

    Re: using a configuration file

    Quote Originally Posted by roidayan View Post
    if i run the script from python shell before making it sis
    it will create/read the config from the same dir as the script file ?
    No. It will go into the directory the PythonScriptShell was installed to - something like E:\Private\2000b1a5.

    If you want to save to the directory where the currently running script is located, then try...

    Code:
    import sys, os
    
    CONFIG_FILENAME = 'config.cfg'
    
    try:
        raise Exception
    catch Exception:
        filepath = sys.exc_info()[2].tb_frame.f_code.co_filename
    filedir, filename = os.path.split(filepath)
    configfile = os.path.join(filedir, CONFIG_FILENAME)
    f = open(configfile, 'w')
    # etc.

  9. #9
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    56

    Re: using a configuration file

    seems sys.exc_info always gives me (None, None, None)
    tried also os.path.dirname(sys.argv[0]) which gives the pythonshell dir i think

  10. #10
    Regular Contributor
    Join Date
    Mar 2003
    Location
    UK
    Posts
    125

    Re: using a configuration file

    Quote Originally Posted by roidayan View Post
    seems sys.exc_info always gives me (None, None, None)
    It will only return something meaningful when called inside an 'except' block. Looks like I erroneously used the word 'catch' rather than 'except' in the code I previously posted. Try the following code verbatim...

    Code:
    try:
        raise Exception
    except Exception:
        print sys.exc_info()
    If that prints (None, None, None), then there's something seriously wrong with Python.


    Quote Originally Posted by roidayan View Post
    tried also os.path.dirname(sys.argv[0]) which gives the pythonshell dir i think
    Indeed, that's why the above code uses the traceback object to find the filename of the currently running script, which, as far as I know, is the only reliable way to get it.

    When packed as a SIS file, sys.argv[0] will point to the default.py script, but when run from the PythonScriptShell, it will point to the PythonScriptShell's default.py, which is not the file you want.

  11. #11
    Super Contributor
    Join Date
    Mar 2003
    Location
    Espoo, Finland
    Posts
    976

    Thumbs up Re: using a configuration file

    Impressive, very many thanx !!! Been looking for this for months !!! ...got close, but never thought about raising an exception by myself !!! Can't wait the evening to try it out

    Cheers,

    --jouni

  12. #12
    Regular Contributor
    Join Date
    Jan 2004
    Location
    Helsinki
    Posts
    376

    Re: using a configuration file

    Quote Originally Posted by JOM View Post
    Impressive, very many thanx !!! Been looking for this for months !!! ...got close, but never thought about raising an exception by myself !!! Can't wait the evening to try it out

    Cheers,

    --jouni
    You can do this if you import the module

    Code:
    import os, sys
    
    mod = sys.modules[__name__]
    file = mod.__file__
    path = os.path.dirname(file)
    Mikko Ohtamaa

    http://mfabrik.com
    http://blog.mfabrik.com

Similar Threads

  1. File reading & writing help, wav file redaing help
    By shubhamlahoti in forum Mobile Java General
    Replies: 6
    Last Post: 2007-06-27, 09:07
  2. Nokia Image Converter
    By davidpurdie in forum General Development Questions
    Replies: 0
    Last Post: 2004-02-18, 15:31
  3. Series 60 SDK Appwizard on windows xp
    By moonjoor in forum Symbian Tools & SDKs
    Replies: 15
    Last Post: 2003-12-24, 10:22

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •