Holger Holger - 8 months ago 112
Python Question

Calling defrag with subprocess.Popen on Windows 7

After a long running process I want to call the defragmentation tool

defrag
on
Windows 7
using
Python
subprocess.Popen
as illustrated in the example.

from subprocess import Popen

process1 = Popen('defrag', shell = True)
process1.wait()
print('finished defragmentation!')


This gives following output:

Der Befehl "defrag" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
finished defragmentation!


Sorry for the German output. Don't know how to switch to English. However, this means that the command
defrag
is not found. I tried it with
shell=False
and with a sequence as input argument. Nothing worked, but I remember that I did the same on
Windows XP
. Also calling
dir
with
Popen
works. Why doesn't
defrag
work on
Windows 7
? How can I make it run?

Answer Source

Now I can answer my own question. The problem is that I am running 32 bit Python on a 64 bit Windows machine. Using a 64 bit Python installation everything runs fine.