0

I have this piece of code that works in Python on my Raspberry Pi:

a1 = "arecord --device=hw:0,0 --format S16_LE --rate 44100 -c1 test.wav"
proc = subprocess.Popen(a1.split())
time.sleep(5)
proc.kill()

When this executes, the USB sound card I have starts to record audio, and then stops after 5 seconds. But I am trying to execute another command line command using the same format, and it doesn't work:

a2 = "rm -f Test/*"
delete_contents = subprocess.Popen(a2.split())

I am trying to call this to delete everything within my Test folder, and it works when I type the command in the command prompt, but will not delete when I call it from my Python program.

smartzer
  • 89
  • 9
  • 2
    Use [shutil.rmtree](https://docs.python.org/2/library/shutil.html), it's simpler. – Hai Vu Apr 19 '17 at 21:27
  • 2
    Also, [`shlex.split()`](https://docs.python.org/2/library/shlex.html#shlex.split) is far more reliable for parsing shell commands. – TemporalWolf Apr 19 '17 at 21:30
  • I tried shutil.rmtree and nothing happens. And by the way, I only want to delete the contents of the Test folder, not the folder itself – smartzer Apr 19 '17 at 21:39
  • Possible duplicate of [Calling rm from subprocess using wildcards does not remove the files](http://stackoverflow.com/questions/11025784/calling-rm-from-subprocess-using-wildcards-does-not-remove-the-files) – matusko Apr 19 '17 at 21:45

1 Answers1

0

os.remove deletes files:

directoryToClean = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'Test')
directoryItems = [os.path.join(directoryToClean, f) for f in os.listdir(directoryToClean)]
[os.remove(f) for f in directoryItems if os.path.isfile(f)]
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47