1

I have a directory of .jpg files I need to loop through, and it has to be invoked from the command line with python program.py directory/*.jpg The code I have only finds the first image in that folder.

import sys, glob

arg = sys.argv[1]
files = glob.glob(arg)
for fname in files:
    print(fname)

It only prints out the name of the first file in the folder, when I print the length of the list files, it returns 1.

Edit: print(arg) is printing the name of the first file, rather than the directory, how do I fix this, sys.argv[1] should be the directory/*.jpg no?

Chaz
  • 195
  • 4
  • 17
  • Not much to be said without looking at your directory/directory structure – cs95 Nov 02 '17 at 13:42
  • At the risk of asking a foolish question, are you absolutely sure that there's more than 1 jpg in that dir? – Jared Smith Nov 02 '17 at 13:43
  • May be `walk()` method of `os` module can help you. – GadaaDhaariGeek Nov 02 '17 at 13:43
  • There are 25 jpgs in that dir, I'm almost completely sure it's not a problem with the directory/structure – Chaz Nov 02 '17 at 13:46
  • What is the output of `ls directory/*.jpg` ? What's the directory name where the files reside? – ventsyv Nov 02 '17 at 13:47
  • Possible duplicate of [Find all files in a directory with extension .txt in Python](https://stackoverflow.com/questions/3964681/find-all-files-in-a-directory-with-extension-txt-in-python) – Drag and Drop Nov 02 '17 at 13:51
  • @ventsyv ls directory/*.jpg gives directory/0606163.jpg directory/060611231.jpg etc. as I expected the program is in the directory that contains the directory of the images, is this the problem? – Chaz Nov 02 '17 at 13:55

3 Answers3

1

Nothing wrong with your code, but you are not escaping your wildcards correctly. Try running your script as:

python program.py directory/\*.jpg

When you run python program.py directory/*.jpg the shell expands the wildcard resulting in argv[1] to be the first filename that matched, thus resulting in glob being called not with <directory>/*.jpg but with <directory>/<firstFile>

You can easily see that if you add print arg before the glob call.

For example, if you directory contains two files, 0606163.jpg and 060611231.jpg

python program.py directory/*.jpg will be equivalent to python program.py directory/0606163.jpg directory/060611231.jpg

ventsyv
  • 3,316
  • 3
  • 27
  • 49
  • Thanks, this works. I also solved it by simply putting the sys.argv call in the loop. `for fname in sys.argv[1:]: print (fname)` – Chaz Nov 06 '17 at 09:53
  • @Chaz If you rely on shell variable extension then no need to call glob. – ventsyv Nov 06 '17 at 14:39
0

I personally use os.walk().

import os
for root, dirs, files in os.walk('YOUR_PATH_OR_SYS_ARG_HERE'):
    for file in files:
        if file.endswith('.jpg'):
            print(file)
MattR
  • 4,887
  • 9
  • 40
  • 67
0

You could use listdir perhaps?

import os
jpgFiles = os.listdir('your/path/here')

for file in jpgFiles:
        print(file)
NikoCon
  • 99
  • 1
  • 2
  • 10