Python:os.remove() 和 os.unlink() 之间的区别以及使用哪一个?

标签 python delete-file

我在一个文件夹中有许多文件。我想在处理完每个文件后删除它。

使用 os.remove()os.unlink 有什么区别?哪种方法最适合我的场景?

最佳答案

注意:最初问这个问题时,它有一个 python-2.7 标签,此后已被删除。有关 Python 3 中所做更改的讨论,请参阅此答案的评论。


它们与 Python 2.7 documentation 中描述的相同。 :

os.<b>remove</b>(<i>path</i>) :

Remove (delete) the file path. If path is a directory, OSError is raised; see rmdir() below to remove a directory. This is identical to the unlink() function documented below. On Windows, attempting to remove a file that is in use causes an exception to be raised; on Unix, the directory entry is removed but the storage allocated to the file is not made available until the original file is no longer in use.

Availability: Unix, Windows.

os.<b>unlink</b>(<i>path</i>) :

Remove (delete) the file path. This is the same function as remove(); the unlink() name is its traditional Unix name.

Availability: Unix, Windows.

关于Python:os.remove() 和 os.unlink() 之间的区别以及使用哪一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42636018/

相关文章:

c# - 如何检测文件是否正在使用?

Python/BeautifulSoup - 提取 div 内容检查 h1 文本

python - 有没有办法删除字符串中重复和连续的单词/短语?

python - Numpy Logarithm 适用于 bool Pandas 系列,但不适用于 Dataframe 中的 bool 列

c# - 如何让我的 .NET 应用程序自行清除?

javascript - 使用 Gulp 删除所有有异常的文件

swift - 能够写入/读取文件但无法删除文件 SWIFT

python - 从列表创建 matplotlib 3D 曲面图

python - 安装 Pillow 错误 : Setup script exited with error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

java - 通过Junrar解压rar文件然后删除压缩包