无法从 Python 项目递归删除 pyc 文件

Can't recursively delete pyc files from Python project

我无法从我的 Python 项目中递归删除 pyc 文件。

这是项目:

    Directory: C:\Users\jk025523\projects\ex47


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       10/07/2016     01:52                bin
d-----       10/07/2016     01:52                docs
d-----       10/07/2016     01:52                ex47
d-----       10/07/2016     01:52                tests
-a----       09/07/2016     21:02            521 setup.py

tests目录下确实有一些pyc文件:

    Directory: C:\Users\jk025523\projects\ex47\tests


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       09/07/2016     21:28            180 ex47_tests.py
-a----       09/07/2016     21:28            721 NAME_tests.pyc
-a----       05/07/2016     10:52              0 __init__.py
-a----       05/07/2016     11:37            140 __init__.pyc

然而,当我在 PowerShell 中输入此命令以删除所有 pyc 文件时:

find . -name "*.pyc" -exec rm -rf {}

PowerShell 输出此错误:

FIND: Parameter format not correct

有人知道如何从此 Python 项目中删除所有 pyc 文件吗?

如果您使用的 "find" 命令 运行 与 linux/unix 等价,则您的 --exec rm -rf {} 末尾需要一个分隔符。像这样:

find . -name "*.pyc" -exec rm -rf "{}" \;

我还建议始终将变量括号 ({}) 括在引号中,以确保您删除的内容不会超过您尝试删除的内容。

编辑:看起来您正在使用 Windows find 变体,它不支持您尝试使用的参数。您可以查看 this page 的语法示例。

您可能需要查看 one of these answers 的替代方案。

您尝试使用的命令是 Linux/Unix find command, which doesn't work on Windows (unless you have something like Cygwin installed, which I don't recommend). Windows has a command with the same name,但功能不同(更像 grep)。 PowerShell 没有 find cmdlet 或别名。您将在 PowerShell 中递归删除具有特定扩展名的文件:

Get-ChildItem -Filter '*.pyc' -Force -Recurse | Remove-Item -Force