How To Use Python os.remove Method To Remove Files And Folders

In Python, you can use the os.remove method to remove files and folders, but sometimes it may resulting in some errors, this is because you do not master how to use the os.remove method correctly. This article will introduce how to use the python os.remove method correctly to avoid errors.

1. Python os.remove Method Overview.

  1. Grammar: os.remove(path).
  2. Parameter: Path — file path to remove.
  3. Return value: The method does not return value.

2. How To Use It To Avoid Errors.

  1. Can not use os.remove to delete a folder, otherwise, it will throw an error.
    # The test folder is an empty folder, from the folder permission, we can see that everyone can remove the below folder.
    drwxrwxrwx    2 songzhao  staff       64 Feb 25 09:33 test
    
    # But when we remove it with the python os.remove method, it will through an Operation not permitted error.
    
    >>> import os
    >>> 
    >>> 
    >>> os.remove('./test')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    PermissionError: [Errno 1] Operation not permitted: './test'
    
    # os.remove method can remove a file successfully.
    >>> os.remove('./test/test.php')
    
    # Now the test folder is empty, but when you remove it with os.remove method, it will still throw Operation not permitted error.
    >>> os.remove('./test')
    Traceback (most recent call last):
      File "", line 1, in 
    PermissionError: [Errno 1] Operation not permitted: './test'
    

3. How To Remove A Folder With Files Inside In Python.

  1. We can use python shutil.rmtree method to remove a folder with files inside it recursively.
    >>> import shutil
    >>> 
    >>> shutil.rmtree('./test')
  2. But the python shutil.rmtree method can only be used to remove a folder, when you use it to remove a file, it will throw Not a directory error.
    >>> import shutil
    >>> 
    >>> 
    >>> shutil.rmtree('./test/test.py')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/shutil.py", line 513, in rmtree
        return _rmtree_unsafe(path, onerror)
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/shutil.py", line 374, in _rmtree_unsafe
        onerror(os.scandir, path, sys.exc_info())
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/shutil.py", line 371, in _rmtree_unsafe
        with os.scandir(path) as scandir_it:
    NotADirectoryError: [Errno 20] Not a directory: './test/test.py'

4. Python os.remove Method Example.

>>> import os, sys
>>> 
# Get current python execution path.
>>> curr_path = os.getcwd()
>>> 
>>> print(curr_path)
/Users/songzhao
>>> 
>>> new_path = curr_path + '/test'
>>> 
>>> print(new_path)
/Users/songzhao/test
>>> 
>>> 
# List files in the new directory.
>>> os.listdir(new_path)
['test.py']
>>> 
# Use os.remove to remove a directory, it will throw an error.
>>> os.remove(new_path)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 1] Operation not permitted: '/Users/songzhao/test'
>>> 
# Use os.remove to remove a file will be successful.
>>> os.remove(new_path+'/test.py')
>>> 
>>> 
# After the file has been removed, the directory is empty.
>>> os.listdir(new_path)
[]

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.