944,038 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1874
  • Python RSS
May 30th, 2007
0

can't figure out loop

Expand Post »
Python Syntax (Toggle Plain Text)
  1. ############################################################################
  2. # #
  3. # This program seeks out every copy of "starcraft.exe" in the computer #
  4. # and deletes them. #
  5. # Created by Matt in Python 2.5 May 2007. #
  6. # #
  7. ############################################################################
  8.  
  9. import os
  10. def file_find(folder):
  11. """search for a filename fname starting in folder"""
  12. for root, dirs, files in os.walk(folder):
  13. for file in files:
  14. # make search case insensitive
  15. if fname.lower() == file.lower():
  16. return os.path.join(root)
  17. return None
  18. def deleteFileSearched():
  19. global COUNTER
  20. os.remove(trueResult)
  21. print trueResult + " deleted successfully..."
  22. COUNTER += 1
  23.  
  24. def main():
  25. # file to search for ...
  26. global fname
  27. global folder
  28. global trueResult
  29. global allfiles
  30.  
  31. li2 = ["stardat.mpq", "starcraft.exe", "Local.dll",
  32. "storm.dll", "Smack32.dll"]
  33. li = ["c:\\", "d:\\", "e:\\", "f:\\"]
  34. for fname2 in li2:
  35. fname = fname2
  36. # folder to start search ...
  37. for folder in li:
  38. result = file_find(folder)
  39. if result == None:
  40. print fname, "not found in", folder
  41. continue
  42.  
  43. trueResult = "<A href="file://\\".join((result">\\".join((result, fname))
  44. print "File found -->", trueResult
  45. deleteFileSearched()
  46. return True
  47. COUNTER = 0
  48. killSwitch = main()
  49. while killSwitch == False:
  50. if killSwitch == True:
  51. break
  52. killSwitch = main()
  53. print COUNTER, "files deleted"
  54.  
I need a way to make this loop until it doesn't find any file in any directory.... I can't figure out how to make it do that.
Similar Threads
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
May 30th, 2007
0

Re: can't figure out loop

Perhaps add near the start of main():
found = False
and add the last line to this sequence:
python Syntax (Toggle Plain Text)
  1. print "File found -->", trueResult
  2. deleteFileSearched()
  3. found = True

... and change return True at the end of main() to return found.

So found starts out "False" meaning "none found" and gets set to "True" when anything is found. A "True" return from main() is interpreted as "not done, call me again".

I'd also change the loop at the bottom to be more like:
Python Syntax (Toggle Plain Text)
  1. while main():
  2. pass # True, call again
  3. print COUNTER, "files deleted"

I'm not quite sure why a single pass doesn't do the trick, but that's not my business.
Reputation Points: 94
Solved Threads: 48
Posting Whiz
BearofNH is offline Offline
321 posts
since May 2007
May 30th, 2007
0

Re: can't figure out loop

i'm helping an admin of a network where the users put starcraft everwhere and anywhere, and rename the folders, and have it in more than one place at once. so i need it to run until its found everywhere they hid it and wipe out the critical files.
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
May 30th, 2007
0

Re: can't figure out loop

LOL. Net administration is like cat-herding.

Here's a possibility for you: Starcraft won't run if the user doesn't have admin rights...

Just a thought.

Anyways, consider these two methods:

Python Syntax (Toggle Plain Text)
  1. for file in file_list:
  2. if file somewhere in file_system:
  3. delete it.
vs.

Python Syntax (Toggle Plain Text)
  1. for file in entire_file_system:
  2. if file in file_list:
  3. delete it.

From a theoretical point of view, both are equally time-consuming. But practically, the second one wins hands-down because you are only traversing the file system once, which requires a lot of hard-drive access time.

AND,

the second one is easier to code.

Python Syntax (Toggle Plain Text)
  1. def main():
  2.  
  3. drives = ['c:/','d:/','e:/','f:/']
  4. bad_guys = ["stardat.mpq", "starcraft.exe", "Local.dll",
  5. "storm.dll", "Smack32.dll"]
  6. for drive in drives:
  7. for root, dirs, files in os.walk(drive):
  8. for file in files:
  9. if file in bad_guys:
  10. print 'found %s in %s' % (file, root)
  11. delete_file(os.path.join(root,file))
  12. main()


I left out the counter and most of the logging, but that's the gist.
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Multi-line terminal commands
Next Thread in Python Forum Timeline: combining strings with slash seperater





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC