首页 > Python资料 博客日记

Python os.remove()方法

2023-08-05 15:39:04Python资料围观182

本篇文章分享Python os.remove()方法,对你有帮助的话记得收藏一下,看Python资料网收获更多编程知识

Python的remove()方法删除文件路径。如果路径是一个目录,则引发错误:OSError

语法

以下是remove()方法的语法 -

os.remove(path)

参数

  • path - 这是要删除的路径。

返回值

  • 此方法不返回任何值。

示例

以下示例显示了remove()方法的用法。

# !/usr/bin/python3
import os, sys
os.chdir("d:\\tmp")

# listing directories
print ("The dir is: %s" %os.listdir(os.getcwd()))

# removing
os.remove("test.java")

# listing directories after removing path
print ("The dir after removal of path : %s" %os.listdir(os.getcwd()))

编译并运行上面的程序,将输出以下结果 -

The dir is: [
   'Applicationdocs.docx', 'book.zip', 'foo.txt', 'home', 
   'Java Multiple Inheritance.html', 'Java Multiple Inheritance_files', 
   'java.ppt', 'ParallelPortViewer', 'test.java'
]
The dir after removal of path : [
   'Applicationdocs.docx', 'book.zip', 'foo.txt', 'home', 
   'Java Multiple Inheritance.html', 'Java Multiple Inheritance_files', 
   'java.ppt', 'ParallelPortViewer'
]

版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐