0

I have a application that converts from one photo format to another by inputting in cmd.exe following: "AppConverter.exe" "file.tiff" "file.jpeg"

But since i don't want to input this every time i want a photo converted, i would like a script that converts all files in the folder. So far i have this:

  def start(self):
    for root, dirs, files in os.walk("C:\\Users\\x\\Desktop\\converter"):
     for file in files:
        if file.endswith(".tiff"):
          subprocess.run(['AppConverter.exe', '.tiff', '.jpeg'])

So how do i get the names of all the files and put them in subprocess. I am thinking taking basename (no ext.) for every file and pasting it in .tiff and .jpeg, but im at lost on how to do it.

Engineero
  • 12,340
  • 5
  • 53
  • 75
pythonore
  • 37
  • 1
  • 8
  • possible duplicate of https://stackoverflow.com/questions/3207219/how-to-list-all-files-of-a-directory – mgul Feb 27 '17 at 19:50
  • Unfortunately, that thread doesn't help me much, granted i am beginner. – pythonore Feb 27 '17 at 19:59
  • Possible duplicate of [How to replace (or strip) an extension from a filename in Python?](http://stackoverflow.com/questions/3548673/how-to-replace-or-strip-an-extension-from-a-filename-in-python) – Mad Physicist Feb 27 '17 at 20:23

2 Answers2

0

You could try looking into os.path.splitext(). That allows you to split the file name into a tuple containing the basename and extension. That might help...

https://docs.python.org/3/library/os.path.html

TheWonton
  • 106
  • 3
0

I think the fastest way would be to use the glob module for expressions:

import glob
import subprocess

for file in glob.glob("*.tiff"):
    subprocess.run(['AppConverter.exe', file, file[:-5] + '.jpeg'])
    # file will be like 'test.tiff'
    # file[:-5] will be 'test' (we remove the last 5 characters, so '.tiff'
    # we add '.jpeg' to our extension-less string

All those informations are on the post I've linked in the comments o your original question.

mgul
  • 742
  • 8
  • 27