dd returns 0 from bash, but returns an error if run with subprocess.run()

206 Views Asked by At

I am trying to run dd from a Python script using subprocess.run(). If I run the following command from my terminal, it works fine:

dd if=/dev/zero of=~/file.txt bs=512 count=1000 oflag=dsync

Note: ~/file.txt did not exists before the command, but gets created automatically.

Now, if I fire up my python3 and do

cmd='dd if=/dev/zero of=~/file.txt bs=512 count=1000 oflag=dsync'
import subprocess
ReturnVariable = subprocess.run(cmd, shell=True)

I get

dd: failed to open '~/file.txt': No such file or directory

This happens both if ~/file.txt exists and if it doesn't. It doesn't make much sense anyway since it is the output file, not the input file.

What am I doing wrong? Why does the same command on the same machine not work if I call it via subprocess.run()?

1

There are 1 best solutions below

0
On

subprocess.run() uses /bin/sh as the shell, which is probably not the same as your interactive shell (probably bash).

sh only replaces ~/ with your home directory when it's at the beginning of a word. bash also expands it after = and :. So of=~/file is expanded in bash, but not sh, so it's not expanded when you use subprocess.run().

You can simply use the environment variable directly instead of tilde.

cmd='dd if=/dev/zero of=$HOME/file.txt bs=512 count=1000 oflag=dsync'