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

    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Problems with Deleting Files


    Hey guys,

    I know questions like these are quite common and I've searched the boards for answers. I'm writing a script that will essentially clean a system of files leftover after an uninstall. This is for OS X.

    For example, it deletes a default document located in "UserName/Library/Default Document.doc". It also deletes all the files associated with Application in ~/Library/Preferences and /Library/Preferences. I would also like it to delete keychain entries associated with Application, but that's probably another topic.

    Here's what I've got so far:

    Code:
    #!/usr/bin/python
    import os
    from os.path import expanduser
    
    ## here I really want ALL "com.application.*" files. ##
    defaults = "/Library/Preferences/com.application.file.plist"
    
    ## this is the default location ##
    library = r'/Documents/Default\ Document.doc'
    
    home = expanduser("~")
    defaults_path = os.path.join(home, defaults)
    library_path = os.path.join(home, library)
     
    ## delete defaults ##
    if os.path.isfile(defaults_path):
            os.remove(defaults_path)
    	print("Removed defaults.")
    else:    ## Show an error ##
            print("Error: %s file not found. Do you need to be running sudo?" % defaults_path)
    
    ## delete library ##
    if os.path.isfile(library_path):
            os.remove(library_path)
    	print("Removed library.")
    else:    ## Show an error ##
            print("Error: %s file not found. Do you need to be running sudo?" % library_path)
    
    ## delete keychain ##
    The first delete works fine; the second gives me the error. I think I'm not properly escaping the space. Also, it doesn't look like the expand user code is working at all (the print out is "Error: /Documents/Default\ Document.doc file not found. Do you need to be running sudo?").

    Any thoughts or advice or criticism is greatly appreciated!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    OS X is Open Systems unix (I'm good with that)
    modified by Apple (in which text file line-ends are CR and other changes I don't know about)

    The backslash looks completely wrong. Remove it.
    library = r'/Documents/Default\ Document.doc'

    Rationale: os.remove probably calls unlink (or remove which calls unlink) with the string file name. This is not the same at all as the shell command

    rm 'file name containing white space'

    in which you must quote the characters that the shell would otherwise separate tokens. The shell removes the quoting characters.

    You've actually put a backslash into the file name.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    487
    Rep Power
    33
    The OP want to delete a file with a space in the name "UserName/Library/Default Document.doc". I would suggest that it be tried with a plain space and see what happens. There are also some tricks, er workarounds, that I have seen used.

    name = "UserName/Library/Default" + chr(32) + "Document.doc"

    and some double quoting, but I don't remember which way it went, single, double, space, double, single, or vice-versa
    name = "UserName/Library/Default" + '" "' + "Document.doc"
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0
    Thanks for the replies. I've tried removing the escaping "\" in the string. I also tried putting an r in front to indicate literal. I've also changed how I use expanduser:

    library = expanduser('~/Documents/Default Document.doc')

    Now it's calling the correct path, but I'm still getting the error:

    Error: /Documents/Default Document.doc file not found. Do you need to be running sudo?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0
    Got it. The literal worked with the space. I was using "Default Doc.doc" as an example, but really I wanted to delete a package, which works like a directory. So I used:

    Code:
    if os.path.isdir(library):
         shutil.rmtree(library)
    That did the job.

    Now my goal is to delete ALL files which have the format "com.application.*". Do I need to do something along of the lines of creating an array, then checking for "com.application.*" and adding each item to the array with the full pathname. Then I can iterate through the array in the delete code?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    487
    Rep Power
    33
    see glob.glob which allows meta-characters, something like
    print glob.glob("/path/name/com.application.*") <-- note the asterisk
    Last edited by dwblas; December 6th, 2013 at 11:28 AM.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0
    That does the job nicely, thanks.

    Now it's just the keychains

IMN logo majestic logo threadwatch logo seochat tools logo