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?

Lakshmi Narayanan
  • 5,220
  • 13
  • 50
  • 92

2 Answers2

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

rofls
  • 4,993
  • 3
  • 27
  • 37
  • 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 – Lakshmi Narayanan May 24 '14 at 23:47
  • 1
    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`. – Jonathan Leffler May 24 '14 at 23:49
  • @JonathanLeffler, is that easier that what I'm doing? It sounds easier. – rofls May 24 '14 at 23:57
  • @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 – Lakshmi Narayanan May 24 '14 at 23:58
  • Hi; don't beg to be accepted. No; what I suggest is not easier, but seems to fit what the OP wants better. Mind you, it is not yet clear why the relative names are needed, but that's what is being asked for in the comments — AFAICT. – Jonathan Leffler May 24 '14 at 23:59
  • @LakshmiNarayanan It's going to depend on what's different between the two machines. The hard-coded path name is not a recipe for happiness. It isn't clear where your Python script is. It isn't clear what the second and third arguments to the Python script represent. At the moment, it stands approximately zero chance of being portable between two machines, but that's not very unusual when you're starting out. But we can't help you without more information about what's where. There is no direct relationship between where the shell script is, where the Python script is, or where the files are. – Jonathan Leffler May 25 '14 at 00:03
  • @JonathanLeffler I am sorry for being imperfect there. Please check my edit in question – Lakshmi Narayanan May 25 '14 at 00:05
  • 1
    @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 ;) – rofls May 25 '14 at 00:07
  • The loop is working fine.Thanks @rofls. Am trying to finish it completely. – Lakshmi Narayanan May 25 '14 at 00:10
  • I'm surprised that nobody mentioned the pitfall of this method: filenames starting with a "." are skipped. – Eric Backus Mar 18 '22 at 22:44
  • Well, those are hidden files which you may or may not want to include. But that does seem worth mentioning... updated! :) – rofls Mar 19 '22 at 00:03
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.

Veedrac
  • 58,273
  • 15
  • 112
  • 169