python script , 如何要求用户输入他想要的扩展文件并列出现有文件
python script , how to ask the user to enter the extension file he want and make a list of existing files
我有这个脚本 select 从 selected 路径 selected 类型的文件而不询问用户并且它工作正常。
我想要的是获取用户输入并列出所请求文件类型的列表。
谁能帮我解决这个问题?
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)
我根据这个写了另一个脚本
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)
在第二个脚本中没有任何反应,只是询问用户
print os.path.join(path.name)
应该是 print os.path.join(path, name)
如上。
此外,如果您希望用户只输入扩展名(例如 py
),您可以使用:
if fnmatch(name, "*." + request):
我有这个脚本 select 从 selected 路径 selected 类型的文件而不询问用户并且它工作正常。
我想要的是获取用户输入并列出所请求文件类型的列表。
谁能帮我解决这个问题?
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)
我根据这个写了另一个脚本
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)
在第二个脚本中没有任何反应,只是询问用户
print os.path.join(path.name)
应该是 print os.path.join(path, name)
如上。
此外,如果您希望用户只输入扩展名(例如 py
),您可以使用:
if fnmatch(name, "*." + request):