Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    Essex, VT
    Posts
    5
    Rep Power
    0

    Deleting Files With Python


    I am new to prgramming and I am trying to delete files using python. IS this possible?
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Very possible, a language that can't even manage a few files wold be pretty damn usless and that def aint Python! And you can do this using the remove() function from the os module i.e.

    >>> import os
    >>> os.remove('Path/To/File.ext')
    >>>

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

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    And, of course, shutil.rmtree is a useful function.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    Essex, VT
    Posts
    5
    Rep Power
    0

    Thank You


    Can you use wildcards like C:\Documents and Settings\Chris\My Documents\*.*
    To delete the contents of that directory. Or can you delete whole directories.?
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Code:
    #!/usr/bin/env python             
    import os
    
    def nukedir(dir):
        if dir[-1] == os.sep: dir = dir[:-1]
        files = os.listdir(dir)
        for file in files:
            if file == '.' or file == '..': continue
            path = dir + os.sep + file
            if os.path.isdir(path):
                nukedir(path)
            else:
                os.unlink(path)
        os.rmdir(dir)
    
    nukedir("/home/mb/test");
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    There's probably (okay, definitely) a simpler way of doing that with os.path.walk so you might look into that.
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    In Python you can't delete a directory which contains files (or at least on windows), this means you have to remove them before attempting to delete it or you get an error!

    Anyway my little offering to the convo... this little function works kinda like grep, hence the name grem .

    Code:
    #!/usr/bin/env python
    
    import os
    
    def grem(path, pattern):
    	for each in os.listdir(path):
    		if pattern.search(each):
    			name = os.path.join(path, each)
    			try: os.remove(name)
    			except:
    				grem(name, pattern)
    				os.rmdir(name)
    You pass it a regex it removes anything that matches , little example usage:

    Code:
    regex = re.compile('')
    grem('folder/', regex)
    Mark.
    programming language development: www.netytan.com Hula

  14. #8
  15. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Netytan -- just one quick observation, if you passed a regexp such as *.txt to your function, it appears that it may not recurse through directories correctly. This is the statement:
    Code:
    if pattern.search(each):
    If pattern is not a blank string, then directory names may not match and thus it won't recurse into them.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Good observation Scrorpy . What i wanted to do was to ignore everything which didnt match the pattern , buggers up recursion but hey .

    If you wana fix this just...

    Code:
    #!/usr/bin/env python
    
    import os
    
    def grem(path, pattern):
    	pattern = re.compile(pattern)
    	for each in os.listdir(path):
    		if pattern.search(each):
    			name = os.path.join(path, each)
    			try: os.remove(name)
    			except:
    				grem(name, '')
    				os.rmdir(name)
    Also *.txt isnt a valid regex so if you did use it ud get an error , gotta use valid Python regex which i believe is differant to Unix.

    Mark.
    Last edited by netytan; December 30th, 2003 at 08:15 PM.
    programming language development: www.netytan.com Hula

  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    You seem to believe that you have to ceate your own function to remove a non-empty directory. Does this mean that shutil.rmtree does not work except on a posix system? Looking at the code, it seems pretty universal.
  20. #11
  21. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Dont need to no , its just more fun sometimes ya know . Naw nothiing wrong with the shutil module, it is very cross platform!

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

  22. #12
  23. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    Essex, VT
    Posts
    5
    Rep Power
    0

    Thanks


    Thanks a bunch for all that.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2007
    Posts
    2
    Rep Power
    0

    More deleting


    What wild card do I use if I want to delete a group of files with the same name but different file extension all in the same directory.

    I was trying to use the os.delete command but coulldn't figure it out from there
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2007
    Posts
    2
    Rep Power
    0
    Originally Posted by drewski
    What wild card do I use if I want to delete a group of files with the same name but different file extension all in the same directory.

    I was trying to use the os.delete command but coulldn't figure it out from there
    sorry I meant the os.remove command
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2007
    Posts
    3
    Rep Power
    0
    I was halfway through a little program i'm writing to learn python - I'm new.
    and i came upon the problem of deleting a file after writing and printing it. But the file name is a variable inputted by the user
    I tried to use the os.remove command-
    >>> import os
    >>> os.remove('Path/To/File.ext')
    However the path is always the same but the filename is a variable.
    I tried using something like:
    >>> import os
    >>> os.remove('path/to/' + filename)
    however because there isn't a space after the last / python takes the rest of the line as text and displays a callback error message saying - "cannot find filename 'path/to/filename'
    what do i do to get my code working
    Please help i'm totally stuck
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo