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