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

高洛峰
Release: 2017-01-14 15:21:01
Original
2892 people have browsed it

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template