Python calling shell script - no directory?
In a python script I call a bash script as follows:
subprocess.Popen(["./scan.sh", dir])
Inside that script, there is,
find $1 -name "*013*.txt" > found_files.txt
For some reason, the dir argumen开发者_JAVA技巧t from python is translated into a version with quotes inside the bash script. Printing 'dir' in python yields the exact path as the user typed it:
~/Desktop/Files
however, find fails with
find: '~/Desktop/Files' no such directory
Running scan.sh manually with ~/Desktop/Files as the argument works fine. How come quotes are being put around it...?
There aren't. What's happening is that the ~
is not being interpreted, as it's the shell's job to do so. Use os.path.expanduser()
to expand the path before passing it to subprocess
.
use $HOME. the '~' is not expanding in quotes nor in double quotes.
python -c "import subprocess;subprocess.Popen(['./scan.sh', '~'])"
python -c "import subprocess;subprocess.Popen(['./scan.sh', '$HOME'])"
my scan.sh contain:
#!/bin/sh
echo =$1=
The first one print =~=
, the second =/Users/jomo=
.
精彩评论