使用rm命令删除文件

发布于 2024-08-21 05:51:24 字数 268 浏览 6 评论 0原文

我想确保删除了所需的文件。 我有类似的代码

dir="/some/path/"
file = "somefile.txt"
cmd_rm= "rm -rf "+dir + file
os.system(cmd_rm)

dirfile 值是从数据库中获取的。如何确保我永远不会结束运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

离去的眼神 2024-08-28 05:51:24

如果您只想删除单个文件,请不要使用 -r 开关。另外,文件名中可能有空格。

使用 Python 的 os 模块中的函数:

dirname = "/some/path/"
filename = "somefile.txt"
pathname = os.path.abspath(os.path.join(dirname, filename))
if pathname.startswith(dirname):
   os.remove(pathname)

最好 路径 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:

dirname = "/some/path/"
filename = "somefile.txt"
pathname = os.path.abspath(os.path.join(dirname, filename))
if pathname.startswith(dirname):
   os.remove(pathname)

Normalizing the path with abspath and comparing it against the target directory avoids file names like "../../../etc/passwd" or similar.

甚是思念 2024-08-28 05:51:24

You might consider using os.remove() instead since it's a great deal less dangerous than what you're attempting.

勿忘初心 2024-08-28 05:51:24

首先,我建议您使用 os.remove() 和 os.rmdir() 函数来处理类似的事情。您最终将获得更可移植的代码,并减少检查命令返回的麻烦。

要检查您正在有效尝试删除的内容(您可能不想只检查“/”),您可以在生成的路径上使用一些正则表达式,或者仅向从数据库返回的所有路径添加基本路径(取决于您的身份)正在做 ...)。

First, I suggest you to use the os.remove() and os.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 ...).

若沐 2024-08-28 05:51:24

按照 Dave Kirby 的说法使用 shutil.rmtree。如果您只想删除文件,请使用:

dir = "/some/path/" 
file = "somefile.txt" 
cmd = os.path.join(dir, file) 
shutil.rmtree(cmd) 

如果您想删除目录,请使用:

dir = "/some/path/" 
file = "somefile.txt"  
shutil.rmtree(dir) 

如果文件受到写保护,请确保您在运行此命令之前具有写权限。

Use shutil.rmtree as Dave Kirby says. If you want to delete the just the file use:

dir = "/some/path/" 
file = "somefile.txt" 
cmd = os.path.join(dir, file) 
shutil.rmtree(cmd) 

If you want to delete the directory use:

dir = "/some/path/" 
file = "somefile.txt"  
shutil.rmtree(dir) 

If the files are write protected make sure you have write permissions before you run this.

别闹i 2024-08-28 05:51:24

有一个名为 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 use shutil.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.

失眠症患者 2024-08-28 05:51:24

假设您提到的 rm -rf 不仅仅是随机的,而且正是您需要的命令,为什么不直接调用它呢?有一个名为 sh 的库允许与 shell 进行更好的集成。

from sh import rm

path_to_delete = '/some/path'
if os.path.exists(path_to_delete):
    rm('-rf', path_to_delete)

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.

from sh import rm

path_to_delete = '/some/path'
if os.path.exists(path_to_delete):
    rm('-rf', path_to_delete)

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 ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文