python - Tilde (~) isn't working in subprocess.Popen() -
when run in ubuntu terminal:
sudo dd if=/dev/sda of=~/file bs=8k count=200k; rm -f ~/file it works fine.
if run through pythons subprocess.popen():
output, err = subprocess.popen(['sudo', 'dd', 'if=/dev/' + disk, 'of=~/disk_benchmark_file', 'bs=8k', 'count=200k'], stderr=subprocess.pipe).communicate() print err it doesn't work. error is:
dd: failed open '~/disk_benchmark_file': no such file or directory
if change in popen() call tilde ~ /home/user, works!
why that? and more important me: how can make work? don't know user name in production.
you need wrap pathnames os.path.expanduser():
>>> import os >>> os.path.expanduser('~/disk_benchmark_file') '/home/dan/disk_benchmark_file' in code occurrence of:
['sudo', 'dd', 'if=/dev/' + disk, 'of=~/disk_benchmark_file', 'bs=8k', 'count=200k'] should replaced with:
['sudo', 'dd', 'if=/dev/' + disk, 'of=' + os.path.expanduser('~/disk_benchmark_file'), 'bs=8k', 'count=200k']
Comments
Post a Comment