Search code examples
pythonpython-2.7searchoperating-systemuser-input

python script , how to ask the user to enter the extension file he want and make a list of existing files


i have this script that select from a selected path a selected type of files without asking the user and it work fine.

what i want is the ability to get the user input and make a list of requested type of files.

can anyone help me with this?

fileSearch.py

#!/usr/bin/python

import os
from fnmatch import fnmatch
root = 'C:\PythonWorkspace'
extension = "*.py"
for path, subdirs, files in os.walk(root):
    for name in files:
        if fnmatch(name, extension):
            print os.path.join(path, name)

I wrote another script based on this one

newFileSearch.py

#!/usr/bin/python

import os
from fnmatch import fnmatch

root2 = 'C:\Users'

request = raw_input("Select the extension you want!")

for path, subdirs, files in os.walk(root2):
    for name in files:
        if fnmatch(name, request):
            print os.path.join(path.name)

in the second script nothing happen it just ask user


Solution

  • print os.path.join(path.name) should be print os.path.join(path, name) as above.

    Also, if you want the user to just input the extension (for instance py) you can use:

    if fnmatch(name, "*." + request):