-2

What is the problem

When I try to run a python script that is suppose to find files in my C drive it returns with an error. I tried python 2.7 and 3.8 (I need python 2.7 so I thought that maybe my python was too old but I tried on the latest version and it still failed)

What is the main goal

I am trying to learn how I can create a function or script in python that returns a file with directory path. The ultimate goal is to create a search function that finds a file (an mp4 type file to be specific) then play that file using os.system or any other module. I am doing this first so I can learn how to create what I am looking for over time. I am hoping to understand this concept first.

What have I tried

I tried looking up a couple of different youtube videos but its hard as the stuff I find is usually more complex than what I am looking for. Here are the videos I saw:

https://www.youtube.com/watch?v=y_BGU-2ZL-Q - This is perfect but the code didn't work for me. (See below)

https://www.youtube.com/watch?v=IWDC9vcBIFQ - This is good but I don't think I need to index or create a gui. So I stopped watching as I wasn't sure how I can apply this to a basic script.

What is the code I am using:

    def find(name):
        count=0
        for root, dirs, files in os.walk('C:\'):
            if name in files:
                print(root,name)
                count+=1
        print("We found "+ count+" results")
        print("Finish")
        input()
    
    try:
        s=input("name: ")
        find(s)
    except:
        print("Error")

Pictures of the error (I forgot to add this into my post the first time):

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • 2
    ‘Doesn’t work’ is far too vague: If you get an error message then edit the _full_ error message into your question. Or what is the output? Try `’c:\\’`? – DisappointedByUnaccountableMod Dec 20 '20 at 17:57
  • Does this answer your question? [Build a full path to Windows file in Python](https://stackoverflow.com/questions/36561442/build-a-full-path-to-windows-file-in-python) – wjandrea Dec 20 '20 at 18:10
  • 1. I forgot to put in picture that explains the error. 2. C:\\ doesn't work. Here is a picture that shows it doesn't work. https://i.ibb.co/m9sgbNt/help3.png –  Dec 20 '20 at 18:42

3 Answers3

1

Correct answer has already been given by @lukover and @barny

Alternatively, in this case, just do away with back slash and replace with forward slash ("/"). Forward slash works for windows and linux both, since I guess, it internally normalizes the path on windows. However if you have a longer path, then consider using os.path.join( ) and then wrap it from outside by os.walk( ).

In nutshell, in this particular case, you may alternatively try the following. Additionally there was an error in the print statement, where you are using + operator to join string and integer. Not allowed.

import os
def find(name):
    count=0
    for root, dirs, files in os.walk('C:/'):
        for file in files:
            if name in file:
                print(root,name)
                count+=1
    print("We found "+ str(count)+" results")
    print("Finish")
    input()

try:
    s=input("name: ")
    find(s)
except Exception as e:
    print(e)
    print("Error")
Amit
  • 2,018
  • 1
  • 8
  • 12
  • This didn't work. Also I apologize, my first post didn't have the picture that shows the error. Here is a link to the image: https://i.ibb.co/jTFf349/help2.png –  Dec 20 '20 at 18:34
  • 1
    do you have "import os" at the top? – Amit Dec 20 '20 at 18:42
  • Thanks for checking. I checked my script and it did have import os at the top. I just stupidly didn't copy it into the code into the post. I am not used to StackOverflow or scripting so it took a while just to make the post the way the system wanted. I also edited my post to show I have import os. I just think its not possible this way honestly. I have dealt with slash issues before but I tried every slash I could think of and it doesn't work. –  Dec 20 '20 at 18:51
  • This line is causing error "print("We found "+ count+" results")" .. you are concatentaing a string with integer. Changing my answer .. – Amit Dec 20 '20 at 18:53
  • Thanks again! - Unfortunately that didn't work either. I copied an pasted exactly the same script to make sure I didn't miss anything (but I added import os at the top) Here is a picture of what I tried: https://i.ibb.co/Xj4ThR9/helpa.png thanks for looking into this. Even if its not possible, I thank you for trying! –  Dec 20 '20 at 19:03
  • The only thing I can think of is you have not saved the file yet. Nothing wrong with the code now. You can modify "except:" with "except Exception as e:" and then "print(e)" in the line immediately below ..paste the output – Amit Dec 20 '20 at 19:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226207/discussion-between-amit-and-jaton-jameer-justice). – Amit Dec 20 '20 at 19:12
0

Change for root, dirs, files in os.walk('C:\'): to for root, dirs, files in os.walk('C:\\'):.

Note the double backslash.

By placing an \ you are escaping the next symbol, so the string never ends. You can even see this in the highlighting in your question.

lukover
  • 55
  • 6
  • I tried C:\\ and it didn't work: https://i.ibb.co/m9sgbNt/help3.png –  Dec 20 '20 at 18:44
0

Try this

import os

def find(name):
    count = 0
for root, dirs, files in os.walk('C:\\'):
    if name in files:
        print(root, name)
    count += 1
    print("We found " + str(count) + " results")
    print("Finish")
    input()


s = input("name: ")
find(s)

It is better not to use simply

 try: #code
 except: print(':(')

You can do this

 try: #code
 except Exception as e: print(e)

Or just leave it at that

 #code

to know the recommendations for correction