#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Location
    Austin, TX
    Posts
    130
    Rep Power
    28

    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:

    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.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    France
    Posts
    53
    Rep Power
    14
    Originally Posted by jread
    I tried using the "glob" function but it then tells me that I can't delete a "list".
    Tell me if I'm wrong but you probably tried something like this :

    python Code:
     
    import os, glob
     
    os.remove( glob.glob('new*.mxd') )


    The correct way to do it is

    python Code:
     
    for filename in glob.glob('new*.mxd') :
        os.remove( filename )


    Moreover,
    Originally Posted by jread
    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
    os.remove(oldmxd) can't work because the remove function expects a string as argument and here, oldmxd is a tuple.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Location
    Austin, TX
    Posts
    130
    Rep Power
    28
    Originally Posted by Zoso
    Tell me if I'm wrong but you probably tried something like this :

    python Code:
     
    import os, glob
     
    os.remove( glob.glob('new*.mxd') )


    The correct way to do it is

    python Code:
     
    for filename in glob.glob('new*.mxd') :
        os.remove( filename )



    Moreover,


    os.remove(oldmxd) can't work because the remove function expects a string as argument and here, oldmxd is a tuple.
    Thank you so much!!

    Yes, I kept getting the "tuple" error when trying it the other way. What is a tuple anyway?

    Again, I really appreciate your help
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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 .

    Code:
    >>> a_tuple = (1, 2, 3, 4)
    >>> a_tuple
    (1, 2, 3, 4)
    >>> type(a_tuple)
    <type 'tuple'>
    >>>
    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    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.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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

    Later,

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo