| | |
can't figure out loop
![]() |
Python Syntax (Toggle Plain Text)
############################################################################ # # # This program seeks out every copy of "starcraft.exe" in the computer # # and deletes them. # # Created by Matt in Python 2.5 May 2007. # # # ############################################################################ import os def file_find(folder): """search for a filename fname starting in folder""" for root, dirs, files in os.walk(folder): for file in files: # make search case insensitive if fname.lower() == file.lower(): return os.path.join(root) return None def deleteFileSearched(): global COUNTER os.remove(trueResult) print trueResult + " deleted successfully..." COUNTER += 1 def main(): # file to search for ... global fname global folder global trueResult global allfiles li2 = ["stardat.mpq", "starcraft.exe", "Local.dll", "storm.dll", "Smack32.dll"] li = ["c:\\", "d:\\", "e:\\", "f:\\"] for fname2 in li2: fname = fname2 # folder to start search ... for folder in li: result = file_find(folder) if result == None: print fname, "not found in", folder continue trueResult = "<A href="file://\\".join((result">\\".join((result, fname)) print "File found -->", trueResult deleteFileSearched() return True COUNTER = 0 killSwitch = main() while killSwitch == False: if killSwitch == True: break killSwitch = main() print COUNTER, "files deleted"
Perhaps add near the start of main():
and add the last line to this sequence:
... and change
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:
I'm not quite sure why a single pass doesn't do the trick, but that's not my business.
found = Falseand add the last line to this sequence:
python Syntax (Toggle Plain Text)
print "File found -->", trueResult deleteFileSearched() 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)
while main(): pass # True, call again print COUNTER, "files deleted"
I'm not quite sure why a single pass doesn't do the trick, but that's not my business.
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
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:
vs.
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.
I left out the counter and most of the logging, but that's the gist.
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)
for file in file_list: if file somewhere in file_system: delete it.
Python Syntax (Toggle Plain Text)
for file in entire_file_system: if file in file_list: 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)
def main(): drives = ['c:/','d:/','e:/','f:/'] bad_guys = ["stardat.mpq", "starcraft.exe", "Local.dll", "storm.dll", "Smack32.dll"] for drive in drives: for root, dirs, files in os.walk(drive): for file in files: if file in bad_guys: print 'found %s in %s' % (file, root) delete_file(os.path.join(root,file)) main()
I left out the counter and most of the logging, but that's the gist.
![]() |
Similar Threads
- Exponitial Loop (C++)
- Loop help (C++)
- changing loops (for to while) and (recursive to for) (C)
- Inheritance and Polymorphism (C++)
- Im completely lost on this one (C)
- newbie-How to multipe InputBoxes (Visual Basic 4 / 5 / 6)
- Using a for loop to sum an integer n and call function add_it (C)
- Pythagorean Triples (C)
- Mortgage Program help (Java)
- How do you output to the screen? (Java)
Other Threads in the Python Forum
- Previous Thread: Multi-line terminal commands
- Next Thread: combining strings with slash seperater
| Thread Tools | Search this Thread |
abrupt accessdenied anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui heads homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl python random recursion redirect remote reverse scrolledtext session simple software sprite statictext string strings syntax table terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython





