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:

import os, sys, shutil, time

# Define variables
date = time.strftime("%m-%d-%y")
oldmxd = ('new*.mxd')
mastermxd = ("master.mxd")
newmxd = ("new" + date + ".mxd")

# Delete the old .mxd
os.remove(oldmxd)

# Create the new .mxd
shutil.copy(mastermxd, newmxd)


print "New .mxd created: " + newmxd

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.

Recommended Answers

All 4 Replies

Will this help?

# module datetime allows you to do calendar math
# corrects for jump to previous month and even leap years

import datetime

today = datetime.date.today()
one_week_ago = (today + datetime.timedelta(days=-7))
two_weeks_ago = (today + datetime.timedelta(days=-14))

# make filenames
oldmxd1 = "new" + one_week_ago.strftime("%m-%d-%y") + ".mxd"
print "oldmxd1 =", oldmxd1
oldmxd2 = "new" + two_weeks_ago.strftime("%m-%d-%y") + ".mxd"
print "oldmxd2 =", oldmxd2
newmxd = "new" + today.strftime("%m-%d-%y") + ".mxd"
print "newmxd =", newmxd

"""
eg. results =
oldmxd1 = new03-22-06.mxd
oldmxd2 = new03-15-06.mxd
newmxd = new03-29-06.mxd
"""

Will this help?

# module datetime allows you to do calendar math
# corrects for jump to previous month and even leap years

import datetime

today = datetime.date.today()
one_week_ago = (today + datetime.timedelta(days=-7))
two_weeks_ago = (today + datetime.timedelta(days=-14))

# make filenames
oldmxd1 = "new" + one_week_ago.strftime("%m-%d-%y") + ".mxd"
print "oldmxd1 =", oldmxd1
oldmxd2 = "new" + two_weeks_ago.strftime("%m-%d-%y") + ".mxd"
print "oldmxd2 =", oldmxd2
newmxd = "new" + today.strftime("%m-%d-%y") + ".mxd"
print "newmxd =", newmxd

"""
eg. results =
oldmxd1 = new03-22-06.mxd
oldmxd2 = new03-15-06.mxd
newmxd = new03-29-06.mxd
"""

Thank you for your help but I'm not sure how this is working. It's creating three new files, yes? What I'm trying to do is only create one new file and find all the old ones and delete them.

Thanks again and hopefully there is a way to figure this out :)

If your program runs religiously every week, as you stated, then the filenames of the old data files are given, so you can erase them if needed.

If your program runs religiously every week, as you stated, then the filenames of the old data files are given, so you can erase them if needed.

Aha! Now I see!

Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.