2

I can't recursively remove the pyc files from my Python project.

Here is the project:

    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

There are indeed some pyc files inside the tests directory:

    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

However when I enter this command in PowerShell to remove all the pyc files:

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

PowerShell outputs this error:

FIND: Parameter format not correct

Anybody know how I can remove all the pyc files from this Python project?

JJJ
  • 32,902
  • 20
  • 89
  • 102
Riz
  • 53
  • 6

2 Answers2

6

The command you're trying to use is the 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, but different functionality (works more like grep). PowerShell does not have a find cmdlet or alias. You'd do recursive deletion of files with a particular extension like this in PowerShell:

Get-ChildItem -Filter '*.pyc' -Force -Recurse | Remove-Item -Force
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
1

If the "find" command you are running is of the linux/unix equivalent, your --exec rm -rf {} will need a delimiter at the end. Like so:

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

I would also recommend always wrapping the variable brackets ({}) in quotes to ensure you are not deleting more than what you are trying to delete.

EDIT: Looks like you are using the Windows find variant, which does not support the arguments you are attempting to use. You can see this page for syntax examples.

You may want to look at one of these answers for alternatives.

Community
  • 1
  • 1
Liam Stanley
  • 1,961
  • 1
  • 11
  • 11
  • Hi - thanks! I tried your solution and Powershell just outputted the following error: I tried wrapping the variable brackets in quotes and also tried leaving the variable brackets unwrapped by quotes. Both times Powershell outputted this error: `FIND: Parameter format not correct` – Riz Jul 10 '16 at 01:15
  • Thanks for the edit @Liam . I will do some research and find out how to delete all the pyc files within Powershell. It looks like I was running the "find" command that works for Bash as opposed to Powershell. If you know how to make this work in Powershell, I'd appreciate any insight you may have. Either way, thanks! – Riz Jul 10 '16 at 01:35