简体   繁体   English

在 python 中大量删除所有文件的最快方法?

[英]Fastest way to delete all files in large volume in python?

I want to completely clear the content of a very large Linux volume containing a huge number of small files.我想完全清除包含大量小文件的非常大的 Linux 卷的内容。 I know how to delete files, but just doing a for loop that calls delete on each one is very slow.我知道如何删除文件,但只是做一个调用 delete 的 for 循环非常慢。

I'd just send a command down to bash to use bash tools, but were running in a docker alpine Linux container, so all the tools I would use don't exist.我只是将命令发送到 bash 以使用 bash 工具,但在 docker alpine Linux 容器中运行,因此我将使用的所有工具都不存在。 I suppose I could change the docker file to ensure their there but that's feeling a bit ugly.我想我可以更改 docker 文件以确保它们在那里,但这感觉有点难看。

Not sure about speed of this, but I guess you could try if you wanted.不确定这样做的速度,但我想如果你愿意,你可以试试。

Here is an example of how you could use shutil.rmtree to delete the contents of a directory:以下是如何使用shutil.rmtree删除目录内容的示例:

import shutil

# Replace '/path/to/dir/' with the path of the directory you want to delete contents from
shutil.rmtree('/path/to/dir/')

This function will delete all subdirectories and files.此 function 将删除所有子目录和文件。 It is important to use this function with caution, as it will delete the all contents permanently.请务必谨慎使用此 function,因为它会永久删除所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM