1

I am writing a bash script (e.g. program.sh) where I am calling a python code in which a list of files are read from a directory.

the python script (read_files.py) is as following:

import os


def files(path):

    for filename in os.listdir('/home/testfiles'):
        if os.path.isfile(os.path.join('/home/testfiles', filename)):
            yield filename

for filename in files("."):

    print (filename)

Now I want to keep the string filename and use it in the bash script.

e.g. program.sh:

#!/bin/bash
python read_files.py

$Database_maindir/filename

.
.
.

How could I keep the string filename (the names of files in the directory) and write a loop in order to execute commands in bash script for each filename?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Nat
  • 325
  • 2
  • 13

2 Answers2

1

The Python script in the question doesn't do anything that Bash cannot already do all by itself, and simpler and easier. Use simple native Bash instead:

shopt -s nullglob
for path in /home/testfiles/*; do
    if [[ -f "$path" ]]; then
        filename=$(basename "$path")
        echo "do something with $filename"
    fi
done

If the Python script does something more than what you wrote in the question, for example it does some complex computation and spits out filenames, which would be complicated to do in Bash, then you do have a legitimate use case to keep it. In that case, you can iterate over the lines in the output like this:

python read_files.py | while read -r filename; do
    echo "do something with $filename"
done
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
janos
  • 120,954
  • 29
  • 226
  • 236
  • thank you very much! I followed your suggestion and it worked perfectly! – Nat Dec 15 '18 at 09:43
  • however, I notice now that despite the fact that the loop works , when I run the script it gives an error of unsupported file type (filename) and then it reads the loop correctly. – Nat Dec 15 '18 at 10:06
  • @Nat I don't understand how can it work and not work at the same time. From what you pasted I don't see anything where "unsupported file type (filename)" could come from. That must come from somewhere else you didn't share. – janos Dec 15 '18 at 10:10
0

Are you looking for something like this? =

for filename in $(python read_files.py); do 
    someCommand $filename
done
balki
  • 26,394
  • 30
  • 105
  • 151
  • `for` isn't a good way to iterate over lines (from a file, command output, or whatever), because it'll get confused if any lines contain whitespace and/or shell wildcards (both of which are perfectly legal in filenames). – Gordon Davisson Dec 15 '18 at 01:15