使用rm命令删除文件
我想确保删除了所需的文件。 我有类似的代码
dir="/some/path/"
file = "somefile.txt"
cmd_rm= "rm -rf "+dir + file
os.system(cmd_rm)
dir
和 file
值是从数据库中获取的。如何确保我永远不会结束运行 rm -rf /
?
在执行 rm -rf 之前我应该检查哪些内容?
I want to make sure that I delete required files.
I have code something like
dir="/some/path/"
file = "somefile.txt"
cmd_rm= "rm -rf "+dir + file
os.system(cmd_rm)
The dir
and file
values are fetched from a database. How can I make sure I never end up running rm -rf /
?
What things should I check before doing rm -rf
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您只想删除单个文件,请不要使用
-r
开关。另外,文件名中可能有空格。使用 Python 的
os
模块中的函数:最好 路径
abspath
和将其与目标目录进行比较可以避免使用“../../../etc/passwd”或类似的文件名。Don't use the
-r
switch if you just want to remove a single file. Also, there could be spaces in the file name.Better use the functions in Python's
os
module instead:Normalizing the path with
abspath
and comparing it against the target directory avoids file names like "../../../etc/passwd" or similar.您可以考虑使用
os.remove()
< /a> 相反,因为它比您尝试的危险要小得多。You might consider using
os.remove()
instead since it's a great deal less dangerous than what you're attempting.首先,我建议您使用 os.remove() 和 os.rmdir() 函数来处理类似的事情。您最终将获得更可移植的代码,并减少检查命令返回的麻烦。
要检查您正在有效尝试删除的内容(您可能不想只检查“/”),您可以在生成的路径上使用一些正则表达式,或者仅向从数据库返回的所有路径添加基本路径(取决于您的身份)正在做 ...)。
First, I suggest you to use the
os.remove()
andos.rmdir()
functions for working with things like that. You will end up with more portable code and less headache for checking command return.To check what you are effectively attempting to remove (you may not want to just check "/"), you can use some regular expressions on the generated path or just add a base path to all path returned from you database (depending what you are doing ...).
按照 Dave Kirby 的说法使用
shutil.rmtree
。如果您只想删除文件,请使用:如果您想删除目录,请使用:
如果文件受到写保护,请确保您在运行此命令之前具有写权限。
Use
shutil.rmtree
as Dave Kirby says. If you want to delete the just the file use:If you want to delete the directory use:
If the files are write protected make sure you have write permissions before you run this.
有一个名为
shutil
的模块,它提供类似 shell 的文件操作。如果您想删除一个目录及其中的所有文件和目录,请使用Shutil.rmtree。然而它是在 python 中实现的,所以如果你要删除大量文件,那么生成 rm 可能会更快,但如果路径中有空格,则会失败。
There is a module called
shutil
that provides shell-like file manipulation. If you want to delete a directory and all files and directories in it then useshutil.rmtree
.However it is implemented in python so if you are deleting a huge number of files then spawning rm may be faster, but will fail if the path has a space in it.
假设您提到的 rm -rf 不仅仅是随机的,而且正是您需要的命令,为什么不直接调用它呢?有一个名为 sh 的库允许与 shell 进行更好的集成。
PS 确保您不是 root 和/或要求用户输入时要格外小心。
并且,是的,让这个人吸烟以避免递归删除单个文件;)
Assuming that your mentioning rm -rf is not just at random, but is exactly the command you need, why not just to call it? There is a lib allowing a greater integration with shell called sh.
PS Make sure you are not root and/or ask for user input to be extra cautious.
And, yes, smoke the man to avoid deleting a single file recursively ;)