1

I am trying to execute my python file using a shell script, in Ubuntu 12.04. For doing this, I have the following code, with the help of the 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

I am new to scripting, so please bear with me if there are other mistakes as well. Am welcome to inputs. This is the following error I get

     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'

Basically, I do not want to explicitly mention the path as well. I want to concatenate just the folder name with the current working directory. Can you please tell me how I can do this?

6
  • 2
    You should make it a habit to always enclose file names in quotes, that is use "$f" instead of $f. Commented May 24, 2014 at 23:43
  • thanks @MichaWiedenmann. But am still getting the same error. Showing in single quotes, as shown above. Commented May 24, 2014 at 23:46
  • 1
    You should use a loop like for f in cover_image/*; do … "$f" …; done. That gives what you seem to be asking for (and is portable across machines). Commented May 25, 2014 at 0:13
  • please check edits, if its still not clear. I am sorry for not being perfectly clear. Commented May 25, 2014 at 0:14
  • shux. that simple. Thanks a lot @JonathanLeffler Commented May 25, 2014 at 0:23

2 Answers 2

4

You should try this:

 #!/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

From here: How to get the list of files in a directory in a shell script?

To make it system independent, you can add this to each machine's .bashrc:

export MYDIR="/path/to/local/top/level/dir"

and then your code would be something like:

 #!/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

Or alternatively, using 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

NOTE: this will skip hidden files

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks a lot @rofls. Can you please tell me how I can concatenate path with the current directory ? as in, pwd + cover_image/. Cuz the code has to be system independant
What is the current directory? The code as shown is independent of the current directory unless the message_2.txt and 0.7 are directory-dependent. If you want to use relative names, then cd "${file_path#/*}"; for f in cover_image/*; do …; done.
@JonathanLeffler, is that easier that what I'm doing? It sounds easier.
@JonathanLeffler What I meant is, when I put this same code along with the folder structure, in some other machine, I want it to still run. without changing the path. Is it possible>? Something like $pwd and add /cover_image to that path? I hope I am clear
@LakshmiNarayanan, I'm not entirely sure either of the above solutions give you the system independence you're looking for, but I'm sure you'll figure it out! At least the for loop should be working now ;)
|
1

$file_path matches a single path... the directory's. You'll want to use something like

file_path=/home/itachi/LN_project/cover_image/*

instead to match the contents of the directory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.