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()
?
subprocess.run()
uses/bin/sh
as the shell, which is probably not the same as your interactive shell (probablybash
).sh
only replaces~/
with your home directory when it's at the beginning of a word.bash
also expands it after=
and:
. Soof=~/file
is expanded inbash
, but notsh
, so it's not expanded when you usesubprocess.run()
.You can simply use the environment variable directly instead of tilde.