Results 1 to 6 of 6

Thread: how to delete files in python?

  1. #1
    Join Date
    Jan 2009
    Posts
    59

    how to delete files in python?

    Hi,

    I am new to Python language.

    I just want to know how to delete files in python?

    Please help.

  2. #2
    Join Date
    Jan 2009
    Posts
    85

    Re: how to delete files in python?

    Hi,

    I have a similar question but not like you.
    Well if you want to delete the file then you can simply use the remove() function in python.
    In this way
    Code:
    import os
    os.remove('path/file.extension')
    My question is How to delete a file before writing it in a first place.
    I mean i want to check if the file exist or not at a particular location & then if it exist then I want the script to delete it & write a new file.
    Is this possible?
    Please help

  3. #3
    Join Date
    Jan 2009
    Posts
    66

    Re: how to delete files in python?

    Hi Joyjeet,
    The best solution to your question is to check if the file exist or not if not write a new file if exist then you simple have to open the file with w mode (like "f = open('filename', 'w')")
    This will automatically truncate the existing file.

  4. #4
    Join Date
    Jan 2009
    Posts
    69

    Re: how to delete files in python?

    Well I want to know how to delete a folder will all its contents?
    I mean to say how to delete the folder consisting of all the files & sub folders in it?

  5. #5
    Join Date
    Aug 2010
    Posts
    1

    Re: how to delete files in python?

    How to delete full folder with it's contents?
    Code:
    os.remove('path/folder')
    will remove a folder, but it must be empty. You could probably write something with os.remove and os.walk, but there's also;
    Code:
    shutil.rmtree('path/folder')
    which will delete a folder and all of it's contents (don't forget to import shutil)

  6. #6
    janysek Guest

    Re: how to delete files in python?

    This are he script for delete files in python.
    >>> import os
    >>> file = 'myfile'
    >>> os.remove('hfkg/' + file)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    WindowsError: [Error 2] The system cannot find the path specified: 'hfkg/myfile'
    Last edited by janysek; 11-08-2010 at 11:24 AM.

Similar Threads

  1. Replies: 7
    Last Post: 04-05-2012, 12:37 PM
  2. Replies: 5
    Last Post: 12-11-2011, 10:22 PM
  3. Permission denied in Python when copying files
    By Elijah in forum Software Development
    Replies: 4
    Last Post: 24-04-2009, 11:37 PM
  4. Download Python 3.0 / Python 3000
    By Amaresh in forum Software Development
    Replies: 6
    Last Post: 24-02-2009, 09:28 AM
  5. How To Delete Files Which Windows Wont Delete?
    By Hardik in forum Windows Software
    Replies: 2
    Last Post: 06-03-2007, 12:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,547,929.61689 seconds with 17 queries