简体   繁体   中英

Python - Run a bash script for all files in a directory

I am creating a Python script to act as a wrapper for some common image-related tasks that I have to do, and here's a part where I am stuck at:

There is a Bash script someone wrote for ImageMagick which is located here: http://www.fmwconcepts.com/imagemagick/autocolor/index.php

Essentially, it can only operate on a single file (I cannot pass *.jpg to it, (it fails when I do so) otherwise I wouldn't have this problem) so I need Python to run this line on every file:

~/autocolor.sh -m " + ColorMethod + " -c " + WorkingDirectory + InputFileName + " " + WorkingDirectory + OutputFileName

This is my current block of code:

elif RetouchOption == "04":
    ColorMethod = input("What method will you use (options are gamma, recolor, none)?: ")
    ClipMode = input("What clipping mode will you use (options are together or separate)?: ")
    for f in WorkingDirectory + "*.jpg":
        do
        os.system("sh ~/autocolor.sh -m " + ColorMethod + " -c " + WorkingDirectory + FileName + " " + WorkingDirectory + FileName)

WorkingDirectory is already established as a variable, and ColorMethod and ClipMode are established in that block. What I need to do is get a FileName variable (or some other way to make this code work).

Thanks for the help. Let me know if I didn't supply enough information. I heard os,system is not the preferred way of doing something like this, but it seems to work well so far in executing other commands I have in the same script. so I'll tackle changing that over at another time.

Here are some ideas:

from glob import glob
import subprocess

elif RetouchOption == "04":
    ColorMethod = input("What method will you use (options are gamma, recolor, none)?: ")
    ClipMode = input("What clipping mode will you use (options are together or separate)?: ")
    script = ["sh", "~/autocolor.sh"]
    method = "-m %s" % ColorMethod
    clipmode = "-c %s" % ClipMode
    for filename in glob("*.jpg"):
        subprocess.call(script + [method, clipmode, filename, filename])

glob is great, and subprocess is preferred over os.system as you surmised.

Note that glob without your WorkingDirectory uses the 'current working directory of the script'. To make it use WorkingDirectory, try something like this:

import os

for filename in glob(os.path.join(WorkingDirectory, '*.jpg')):
    ...

Isn't it better to use bash?

for f in *.jpg; do ./autocolor.sh $f; done

for f in glob.glob('*.jpg'):
    do_something() 

This is a suggestion for coding. I write a lot of Python code that works with MySQL. I have found it helpful as a debugging aid to create the string in a variable and print it out, so you can see what the script is doing.

You will do well with earlier answers to your question. They are quite helpful.

Have you considered using os.listdir to get all the files in the directory?

Once you have a listing of all files in the directory, you can pull out all the JPGs and use the following code to to remove the ".jpg" from the end of the filename:

>>> f = 'firstPic.jpg'
>>> f. replace(".jpg", '')
'firstPic'

Hope this helps

The Python glob module makes it easy to create lists that match a pattern.

Example:

    import glob
    glob.glob("*txt")

The above code would return a list of all "txt" files in the current directory.

http://docs.python.org/library/glob.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM