March 29th, 2006, 03:51 PM
How do you get Python to find and delete multiple files?
I am trying to create what should be a very simple script, though I'm having a very difficult time getting it to work. All I want to do is create a new copy of a master file once per week and do my work on the copy. I plan to set this up in task manager to automatically run the script weekly (on Sunday nights for example).
The script will:
- Search for the previous editing file
- Delete the previous editing file
- Copy the master file and create a new editing file
- When creating the new editing file, it will also grab the date of creation and add that to the name of the editing file
Everything works except the part where it searches the old file. Since the file name is going to change weekly (given a new date in the filename each time), it will have to use a wildcard search to make sure it finds the old edit file. For instance, to get rid of these two: "new03-04-06.mxd" & "new03-11-06.mxd", I would need it to look for "new*.mxd"
I tried using the "glob" function but it then tells me that I can't delete a "list". I'm now out of ideas. Hopefully someone on here can help out. I will post the code for the script below:
All help is most appreciated!
P.S. the reason for this script is that our GIS map files (.mxd) tend to lose performance over a period of time, and must be deleted and created fresh again once per week.
March 30th, 2006, 10:31 AM
Tell me if I'm wrong but you probably tried something like this :
Originally Posted by jread
import os, glob
os.remove( glob.glob('new*.mxd') )
The correct way to do it is
for filename in glob.glob('new*.mxd') :
os.remove( filename )
os.remove(oldmxd) can't work because the remove function expects a string as argument and here, oldmxd is a tuple.
Originally Posted by jread
March 30th, 2006, 11:40 AM
Thank you so much!!
Originally Posted by Zoso
Yes, I kept getting the "tuple" error when trying it the other way. What is a tuple anyway?
Again, I really appreciate your help
March 30th, 2006, 11:59 AM
Its an imutable list of sorts. Simply a List that you can't be altered . You create them with the (,) form. As such its probably a bad idea to surround things with () because it becomes comfusing in Python – lisp-like though .
>>> a_tuple = (1, 2, 3, 4)
(1, 2, 3, 4)
March 31st, 2006, 09:39 AM
you can also use tuple() to convert a list to a immutable list...
Netytan stop it with your lisp:P
Those people who think they know everything are a great annoyance to those of us who do.
March 31st, 2006, 10:22 AM
Monkey how does it effect you when I relate things to Lisp? It's the mother of all dynamic languages so can you say it's not relevant, or in this case a valid observation? Your going to have to deal with it .
One more time: Lisp Lisp Lisp