×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    22

    converting file path into a text string

    Hi all

    Is there a way/module to convert a file path into a text string in Python? and how do I go about using it?

    for example, 'C:\folder\helloWorld.txt'

    is there a way to get the file path out in this format in Python?

  2. #2
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: converting file path into a text string

    Hi,

    If I understood what you meant, you can use the functions available in the os module or os.path.

    But please try to explain your question again.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    22

    Re: converting file path into a text string

    Oops... Sorry to make it confusing.

    Like instead of manually keying into Python the file path. Is there a way to get the file path of that file via selecting that file from a list? Something like a file browser that gets the file path instead of the file.

    Hope this explanation is better.

  4. #4
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: converting file path into a text string

    Yes, that can be done with the listdir function from the os module:
    Code:
    import os
    
    file_paths = ["C:\\" + i for i in os.listdir("C:\\")]
    
    #file_paths is now a list of paths of files and folders from C:\
    #like C:\helloWorld.txt, C:\mymusic.mp3 etc.
    You can also have a look at the filebrowser.py example that comes with PyS60.
    Last edited by bogdan.galiceanu; 2009-01-04 at 19:19.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    22

    Re: converting file path into a text string

    Thanks for the help bogdan.

    I am actually trying to modify the filebrowser.py in the S60 emulator to what I want it to be.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    22

    Re: converting file path into a text string

    Hi all

    Can I ask when I use the 'listdir' function, can the file path be imported to another script as text?
    Last edited by unknownxrecon; 2009-01-05 at 05:59.

  7. #7
    Nokia Developer Moderator
    Join Date
    May 2007
    Location
    21.46 N 72.11 E
    Posts
    3,796

    Re: converting file path into a text string

    Quote Originally Posted by unknownxrecon View Post
    Hi all

    Can I ask when I use the 'listdir' function, can the file path be imported to another script as text?
    Yes, as file_paths is a variable, you can import it in other scripts as well.

    Best Regards,
    Croozeys
    Pankaj Nathani
    www.croozeus.com

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    22

    Re: converting file path into a text string

    Hi all

    Is there any command that i can use to just get the file_name when i select it from the list of files that is shown from filebrowser.py?
    Last edited by unknownxrecon; 2009-01-12 at 07:16.

  9. #9
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: converting file path into a text string

    You can get the file name by adding the red line in the filebrowser.py script:
    Code:
    ...
    def lbox_observe(self, ind = None):
            if not ind == None:
                index = ind
            else:
                index = self.lb.current()
            focused_item = 0
    
            if self.current_dir.at_root:
                self.dir_stack.append(index)
                self.current_dir.add(index)
            elif index == 0:                              # ".." selected
                focused_item = self.dir_stack.pop()
                self.current_dir.pop()
            elif os.path.isdir(self.current_dir.entry(index-1)):
                self.dir_stack.append(index)
                self.current_dir.add(index-1)
            else:
                item = self.current_dir.entry(index-1)
                appuifw.note(unicode(os.path.split(item)[1]))
                if os.path.splitext(item)[1] == '.py':
                    i = appuifw.popup_menu([u"execfile()", u"Delete"])
                else:
                    i = appuifw.popup_menu([u"Open", u"Delete"])
                if i == 0:
                    if os.path.splitext(item)[1].lower() == u'.py':
                        execfile(item, globals())
                        self.refresh()
                        #appuifw.Content_handler().open_standalone(item)
                    else:
                        try:
                            appuifw.Content_handler().open(item)
                        except:
                            import sys
                            type, value = sys.exc_info() [:2]
                            appuifw.note(unicode(str(type)+'\n'+str(value)), "info")
                    return
                elif i == 1:
                    os.remove(item)
                    focused_item = index - 1
    
            entries = self.current_dir.list_repr()
            if not self.current_dir.at_root:
                entries.insert(0, (u"..", u""))
            self.lb.set_list(entries, focused_item)
    ...
    os.path.split(path) splits the path given as an argument into 2 parts, the second of which is the file name (along with extension).

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    22

    Re: converting file path into a text string

    Thanks for the tip, Bogdan

    But still, sorry, it does not answer my question.

    I attached the codes of the file browser I am working on now. The browser itself works but the codes on how to get the filepath is what I am not sure of.

    Code:
    import os
    import appuifw
    import e32
    import dir_iter
    import sys
    
    class Filebrowser:
        def __init__(self):
            self.script_lock = e32.Ao_lock()
            self.dir_stack = []
            self.current_dir = dir_iter.Directory_iter(e32.drive_list())
    
        def run(self):
            from key_codes import EKeyLeftArrow
            entries = self.current_dir.list_repr()
            if not self.current_dir.at_root:
                entries.insert(0, (u"..", u""))
            self.lb = appuifw.Listbox(entries, self.lbox_observe)
            self.lb.bind(EKeyLeftArrow, lambda: self.lbox_observe(0))
            old_title = appuifw.app.title
            self.refresh()
            self.script_lock.wait()
            appuifw.app.title = old_title
            appuifw.app.body = None
            self.lb = None
    
        def refresh(self):
            appuifw.app.title = u"File browser"
            appuifw.app.menu = []
            appuifw.app.exit_key_handler = self.exit_key_handler
            appuifw.app.body = self.lb
    
        def do_exit(self):
            self.exit_key_handler()
    
        def exit_key_handler(self):
            appuifw.app.exit_key_handler = None
            self.script_lock.signal()
    
        def lbox_observe(self, ind = None):
            if not ind == None:
                index = ind
            else:
                index = self.lb.current()
            focused_item = 0
    
            if self.current_dir.at_root:
                self.dir_stack.append(index)
                self.current_dir.add(index)
            elif index == 0:                              # ".." selected
                focused_item = self.dir_stack.pop()
                self.current_dir.pop()
            elif os.path.isdir(self.current_dir.entry(index-1)):
                self.dir_stack.append(index)
                self.current_dir.add(index-1)
            else:
                item = self.current_dir.entry(index-1)            
    	
    	    i= appuifw.popup_menu([u"Use this file", u"Cancel"])
    		if i == 0
    		# runs commands that retrieves the filepath of the displayed item from the list. it also sends the filepath to another script
    		else:
    		# returns back to the list of items in the file browser	 
    
            entries = self.current_dir.list_repr()
            if not self.current_dir.at_root:
                entries.insert(0, (u"..", u""))
            self.lb.set_list(entries, focused_item)
    
    if __name__ == '__main__':
        Filebrowser().run()
    By the way, will I be able to use any of the following codes below to get the filepath of the item selected? Sorry for being such a newbie

    Code:
    print 'sys.argv[0]=', sys.argv[0]
    pathname = os.path.dirname(sys.argv[0])
    print 'path=', pathname
    print 'full path=', os.path.abspath(pathname)
    Last edited by unknownxrecon; 2009-01-13 at 05:38. Reason: Grammar
    Yours Sincerely
    Kay

  11. #11
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: converting file path into a text string

    The code I posted works, I tested it. The code has a red line. Add it in filebrowser.py exactly like in the code I posted (or your modified version of it, but that may require other small modifications). os.path.split(path) splits the path given as an argument into 2 parts, the second of which is the file name (along with extension).

    Quote Originally Posted by unknownxrecon View Post
    By the way, will I be able to use any of the following codes below to get the filepath of the item selected?
    Code:
    print 'sys.argv[0]=', sys.argv[0]
    pathname = os.path.dirname(sys.argv[0])
    print 'path=', pathname
    print 'full path=', os.path.abspath(pathname)
    You really should check the Python documentation about those and also experiment with them to see for yourself what they do:
    os.path
    sys

Similar Threads

  1. Problem with pyobfuscate
    By JOM in forum Symbian
    Replies: 3
    Last Post: 2008-06-20, 22:47
  2. Contacts fields order??
    By timatima in forum Symbian
    Replies: 1
    Last Post: 2007-06-08, 13:51
  3. File path problem on the handset
    By symbians60 in forum Symbian
    Replies: 1
    Last Post: 2007-05-04, 09:38
  4. Reading A Text File Woes
    By _nomad_ in forum Symbian
    Replies: 1
    Last Post: 2005-02-02, 10:09
  5. Replies: 0
    Last Post: 2002-07-24, 16:11

Posting Permissions

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