Python:os.remove() 和 os.unlink() 之间的区别,使用哪一个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42636018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: Difference between os.remove() and os.unlink() and which one to use?
提问by NoName
I have a number of files in a folder. I want to delete each file once it has been processed.
我在一个文件夹中有许多文件。我想在处理完每个文件后将其删除。
What's the difference between using os.remove()
and os.unlink
? Which method is ideal for my scenario?
usingos.remove()
和 和有os.unlink
什么不一样?哪种方法最适合我的场景?
回答by shash678
They are identical as described in the official Python 2.7.15 documentation.
它们与官方 Python 2.7.15 文档中描述的相同。
Remove (delete) the file path. If pathis a directory, OSErroris raised; see rmdir()below to remove a directory. This is identical to the unlink()functiondocumented 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.
移除(删除)文件路径。如果路径是目录, 则引发OSError;请参阅下面的rmdir()以删除目录。这与下面记录的unlink()函数相同。在 Windows 上,尝试删除正在使用的文件会导致引发异常;在 Unix 上,目录条目被删除,但分配给文件的存储在原始文件不再使用之前不可用。
可用性:Unix、Windows。
Remove (delete) the file path. This is the same function as remove();the unlink()name is its traditional Unix name.
Availability: Unix, Windows.
移除(删除)文件路径。这与remove() 的功能相同;在取消链接()的名字是它的传统的Unix名称。
可用性:Unix、Windows。
回答by LightCC
When using pathlib.Path
file access in Python v3.4 and higher
pathlib.Path
在 Python v3.4 及更高版本中使用文件访问时
While the question specifically asks for the os
module file removal, the latest versions of Python have another option for removing files that may be an alternative.
虽然这个问题特别要求os
删除模块文件,但最新版本的 Python 有另一种删除文件的选项,这可能是一种替代方法。
Direct Answer - use pathlib.Path.unlink()
直接回答 - 使用 pathlib.Path.unlink()
When using the pathlib modulefor file access, use pathlib.Path.unlink()
to remove files.
使用pathlib 模块进行文件访问时,用于pathlib.Path.unlink()
删除文件。
The Path.unlink()
method is a replacement for both os.remove()
and os.unlink()
. It is executed directly on a Pathobject, rather than being passed the location of a file through a stringargument.
该Path.unlink()
方法是用于两者替换os.remove()
和os.unlink()
。它直接在Path对象上执行,而不是通过字符串参数传递文件的位置。
More details
更多细节
Starting in Python v3.4 the pathlib
builtin module is available to handle file access in an object-oriented manner. I believe a separate package is also available via Pip for older versions of Python.
从 Python v3.4 开始,pathlib
内置模块可用于以面向对象的方式处理文件访问。我相信一个单独的包也可以通过 Pip 用于旧版本的 Python。
With pathlib, you create folder and file objects that are of the Path
class. The related method of removing a file has been consolidated to just unlink()
. They do not have a remove()
method (likely because, per shash678's answer, there is no difference, it's just an alias). This appears to be equivalent to the os
methods of file deletion, other than the underlying means of specifying the file itself.
使用 pathlib,您可以创建属于Path
该类的文件夹和文件对象。删除文件的相关方法已合并为仅unlink()
. 他们没有remove()
方法(可能是因为,根据 shash678 的回答,没有区别,只是别名)。这似乎等同于os
文件删除的方法,而不是指定文件本身的底层方法。
See Object Oriented file system paths, along with the table at the bottom that shows both os.remove()
and os.unlink()
map to Path.unlink()
.
请参阅面向对象的文件系统路径,以及底部显示os.remove()
和os.unlink()
映射到的表Path.unlink()
。
In Python v3.8, a missing_okargument was added to the Path.unlink()
function. When *missing_ok* == True
, an exception will not be raised if the file doesn't exist before trying to remove it.
在 Python v3.8 中,函数中添加了一个missing_ok参数Path.unlink()
。当 时*missing_ok* == True
,如果在尝试删除文件之前文件不存在,则不会引发异常。