lord3nd3r

python search

Dec 11th, 2022
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. import os
  2.  
  3. # Function to search for files matching the given criteria in the specified folder
  4. def search_files(folder, criteria):
  5. # Initialize an empty list to store the search results
  6. results = []
  7.  
  8. # Loop through all files in the specified folder
  9. for filename in os.listdir(folder):
  10. # Check if the file matches the search criteria
  11. if criteria in filename:
  12. # If the file matches the criteria, add it to the results list
  13. results.append(filename)
  14.  
  15. # Return the list of search results
  16. return results
  17.  
  18. # Prompt the user for the search criteria
  19. criteria = input("Enter the search criteria: ")
  20.  
  21. # Set the folder to search
  22. folder = "C:\\MyFolder"
  23.  
  24. # Search for files matching the given criteria in the specified folder
  25. results = search_files(folder, criteria)
  26.  
  27. # Print the top 10 search results
  28. print("Top 10 search results:")
  29. for i in range(min(10, len(results))):
  30. print(f"{i+1}. {results[i]}")
Advertisement
Add Comment
Please, Sign In to add comment