Home  >  Article  >  Backend Development  >  Python solution to error when deleting a folder using os.remove

Python solution to error when deleting a folder using os.remove

高洛峰
高洛峰Original
2017-01-14 15:21:012889browse

os.remove cannot be used to delete folders, otherwise access will be denied.

# -*- coding:utf-8 -*-import osif __name__ == "__main__": os.remove('D:\\test')

Running result:

Python 使用os.remove删除文件夹时报错的解决方法

Delete empty directories:

# -*- coding:utf-8 -*-import osif __name__ == "__main__": os.rmdir('D :\\test')

If the directory is not empty, an error will be reported, as follows:

Python 使用os.remove删除文件夹时报错的解决方法

Delete the directory (regardless of whether the directory is empty):

# -*- coding:utf-8 -*-import shutilif __name__ == "__main__": shutil.rmtree('D:\\test')

The above Python uses os.remove The solution to the error when deleting a folder is all the content shared by the editor. I hope it can give you a reference, and I hope you will support PHP Chinese website more.

For more Python related articles on how to solve the error when deleting a folder when using os.remove, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn