×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: File Search

  1. #1
    Regular Contributor
    Join Date
    May 2008
    Posts
    88

    File Search

    Hi,
    Can nebody suggest me how to check whether a particular file is present in the cell or not?.
    Rishabh

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

    Re: File Search

    Hi,

    It's really a matter of programming a search function. I've made one, though it may not be as efficient as possible but it gets the job done:

    Code:
    import os, appuifw, e32
    
    #Ask for the name of the file to search for, including extension
    f=appuifw.query(u"Enter file name with extension", "text")
    
    z=0
    
    def findfile(folder):
    	global z
            #Search in every folder of every folder
    	for i in os.listdir(folder):
    		p=folder+"\\"+i
    		if(os.path.isdir(p)):findfile(p)
    		elif(os.path.isfile(p)):
    			a=p.split("\\")
    			if(a[-1]==f):
    				appuifw.note(u"File found")
    				z=1
    				break
                                    #If the file is found we display the message and stop looking for it
    
    #Specify the drive to look in; can be C and/or E
    findfile("C:")
    
    #If the file doesn't exist we display the message
    if(z==0):appuifw.note(u"File not found")

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    386

    Re: File Search

    Yes, this is a good solution.

    I tweaked it a bit to be a more clean solution without using global variables.
    Code:
    import os, appuifw
    
    def findfile(folder, filename):
    	for name in os.listdir(folder):
    	    path = os.path.join(folder, name)
    	    if os.path.isdir(path):
    	        path = findfile(path, filename)
    	        if path is not None:
    	            return path
    	    elif os.path.isfile(path) and name == filename:
    	        return path
    
    # Ask for the name of the file to search for, including extension
    filename = appuifw.query(u'Enter file name with extension', 'text')
    
    # Specify the folder to look in
    path = findfile('C:\\', filename)
    
    # Display the path or error message if not found
    if path is None:
        appuifw.note(u'Not found')
    else:
        appuifw.note(u'Found: ' + path)

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

    Re: File Search

    Thanks for improving it. And here's another modification that in most cases saves up to 1 second from the search time:
    Code:
    import os, appuifw
    
    def findfile(folder, filename):
    	for name in os.listdir(folder):
    	    path = os.path.join(folder, name)
    	    if os.path.isfile(path) and name == filename:
    	        return path
    	    elif os.path.isdir(path):
    	        path = findfile(path, filename)
    	        if path is not None:
    	            return path
    
    # Ask for the name of the file to search for, including extension
    filename = appuifw.query(u'Enter file name with extension', 'text')
    
    # Specify the folder to look in
    path = findfile('C:\\', filename)
    
    # Display the path or error message if not found
    if path is None:
        appuifw.note(u'Not found')
    else:
        appuifw.note(u'Found: ' + path)
    Note: You have to specify the file name exactly as it is, this thing is case sensitive. So A.py is not the same as a.py.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    386

    Re: File Search

    Thanks. I have improve your improvement a bit since in your code, the os.path.isdir() will still be called for all files that have an unmaching name.

    This version also ignores the case, so A.py is same as a.py.
    Code:
    import os, appuifw
    
    def findfile(folder, filename):
    	for name in os.listdir(folder):
    	    path = os.path.join(folder, name)
    	    if os.path.isfile(path):
                    if name.lower() == filename:
    	            return path
    	    elif os.path.isdir(path):
    	        path = findfile(path, filename)
    	        if path is not None:
    	            return path
    
    # Ask for the name of the file to search for, including extension
    filename = appuifw.query(u'Enter file name with extension', 'text')
    
    # Specify the folder to look in; filename has to be lower case for
    # case insensitive comparison
    path = findfile('C:\\', filename.lower())
    
    # Display the path or error message if not found
    if path is None:
        appuifw.note(u'Not found')
    else:
        appuifw.note(u'Found: ' + path)
    Lets improve it further . Next thing would be to replace recursiveness with a list. Would it be quicker?

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

    Re: File Search

    Great.

    I have a slight idea of what you mean by using a list, but I'm not sure. Could you please explain?

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    386

    Re: File Search

    Instead of using a recursive function we could use a list as a stack. Upon entering a subdirectory, the current state would be pushed to the stack and restored after the subdirectory is scanned. It is a different approach to the problem and would definitely be longer but it would have no function calls which are said to be pretty slow in Python in general.

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

    Re: File Search

    I see. Yes, that's interesting, like a sort of backtracking. I'm not sure that would help because it does the same thing, but like you said it doesn't call functions. I don't think functions in Python are that slow that it would make a difference.

    But please try to implement it, and if it work share the solution

  9. #9
    Nokia Developer Champion
    Join Date
    Feb 2008
    Location
    Ahmedabad, Gujarat, India
    Posts
    3,852

    Re: File Search

    hi bogdan and Y.A.K
    that seems a really great discussion on coding that will help a lot to other developers.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    386

    Re: File Search

    Ok, here it goes. It's just the function, the rest of the code is same as in my last one.

    Code:
    def findfile(folder, filename):
        stack = [(folder, os.listdir(folder))]
        while stack:
            folder, names = stack[-1]
            while names:
                name = names.pop()
                path = os.path.join(folder, name)
                if os.path.isfile(path):
                    if name.lower() == filename:
                        return path
                elif os.path.isdir(path):
                    stack.append((path, os.listdir(path)))
                    break
            else:
                stack.pop()
    It works but I haven't done any benchmarks. As you can see, it's not recursive. It is also much shorter than I thought it will be, Python keeps suprising me .

  11. #11
    Regular Contributor
    Join Date
    May 2008
    Posts
    88

    Re: File Search

    Hi,
    Thnks for your valuable help.But I was wondering how can I check for all the drives in my cell.How can I know which drives are there in it.
    Rishabh

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

    Re: File Search

    e32.drive_list()

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    386

    Re: File Search

    Read the PyS60 docs, you will find a lot of helpfull info there.

    The list of available drives can be obtained using e32.drive_list() function. From the docs:
    drive list()
    Returns a list of currently visible drives as a list of Unicode strings '<driveletter>:'
    Note that drive D: is always a virtual RAM drive and Z: is ROM, you can't add/change/remove any files there so there is no point searching in it every time.

    You're probably mostly interested in C: which is phone's memory and E: (if available) which is the memory card.

  14. #14
    Regular Contributor
    Join Date
    May 2008
    Posts
    88

    Re: File Search

    Hi,
    One more doubt...how can I search for audio files in my cell.
    Rishabh

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

    Re: File Search

    Quote Originally Posted by rishabhgupta View Post
    Hi,
    One more doubt...how can I search for audio files in my cell.
    Rishabh
    Do you mean mp3 or wav files, for example? If so, simply modify the code we provided.
    Here you just ask for the extension instead of the file's complete name. And just make necessary adjustments for displaying all the files that match the extension.

Similar Threads

  1. Carbide Build PKG file to SISX - Phone says: "Can't Install"
    By mikedeklerk in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 6
    Last Post: 2009-05-19, 08:23
  2. Tranfering file by bluetooth
    By hassan83 in forum Symbian
    Replies: 5
    Last Post: 2008-04-18, 17:01
  3. Help needed with a 3gp file...
    By joedoe_1981 in forum Streaming and Video
    Replies: 0
    Last Post: 2007-07-18, 18:58
  4. Search for a file
    By Amilael in forum Symbian
    Replies: 12
    Last Post: 2006-11-10, 08:26
  5. 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
  •  
×