我试图在Ubuntu 12.04中使用shell脚本执行我的python文件。为此,在link
的帮助下,我有以下代码 #!/bin/bash
file_path=/home/itachi/LN_project/cover_image
for f in $file_path
do
python Question2_lsbreplacement_encode.py $f message_2.txt 0.7
done
我是脚本新手,所以如果还有其他错误,请耐心等待。欢迎投入。这是我得到的以下错误
Traceback (most recent call last):
File "Question2_lsbreplacement_encode.py", line 26, in <module>
img = Image.open(imgname) # reading image
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1955, in open
fp = __builtin__.open(fp, "rb")
IOError: [Errno 21] Is a directory: '/home/itachi/LN_project/cover_image'
基本上,我也不想明确提及路径。我想只将文件夹名称与当前工作目录连接起来。你能告诉我怎么做吗?
答案 0 :(得分:4)
你应该试试这个:
#!/bin/bash
file_path=/home/itachi/LN_project/cover_image
for f in $file_path/*
do
python Question2_lsbreplacement_encode.py $f message_2.txt 0.7
done
从这里: How to get the list of files in a directory in a shell script?
要使系统独立,您可以将其添加到每台机器的.bashrc:
export MYDIR="/path/to/local/top/level/dir"
然后你的代码就像:
#!/bin/bash
file_path=$MYDIR/itachi/LN_project/cover_image
for f in $file_path/*
do
python Question2_lsbreplacement_encode.py $f message_2.txt 0.7
done
或者,使用PWD:
#!/bin/bash
cwd=$(pwd)
file_path=$cwd/cover_image
for f in $file_path/*
do
python Question2_lsbreplacement_encode.py $f message_2.txt 0.7
done
答案 1 :(得分:1)
$file_path
匹配单个路径...目录&#39}。你想要使用像
file_path=/home/itachi/LN_project/cover_image/*
而不是匹配目录的内容。