我在一个文件夹中有许多文件。我想在处理完每个文件后删除它。
使用 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; seermdir()
below to remove a directory. This is identical to theunlink()
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()
; theunlink()
name is its traditional Unix name.Availability: Unix, Windows.
关于Python:os.remove() 和 os.unlink() 之间的区别以及使用哪一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42636018/