I am trying to run command line using subprocess in python and this is my try
import subprocess
#magick mogrify -path C:/Users/Future/Desktop/results -format PNG C:/Users/Future/Desktop/IDs/*.tiff
#Use 'subprocess.run(["magick", "-path", variable, "-format", "png", variable])'
source_path = "C:/Users/Future/Desktop/IDs/*.tiff"
target_path = "C:/Users/Future/Desktop/results"
subprocess.call(["magick mogrify", "-path", target_path, "-format", "PNG", source_path])
The code was created with the help of Mr. @MarkSetchell I got an error like that
Traceback (most recent call last):
File "C:\Users\Future\Desktop\demo.py", line 52, in <module>
subprocess.call(["magick mogrify", "-path", target_path, "-format", "PNG", source_path])
File "C:\Users\Future\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 349, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\Future\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\Future\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
I also tried to use run instead of call but the same problem.
This is my final try
from pathlib import Path
import shutil
import subprocess
BASE_DIR = Path.cwd()
IDs_DIR = BASE_DIR / 'IDs'
RESULTS_DIR = BASE_DIR / 'Results'
try:
shutil.rmtree(RESULTS_DIR)
except OSError as e:
pass
RESULTS_DIR.mkdir(parents=True, exist_ok=True)
#source_path = "C:/Users/Future/Desktop/IDs/*.tiff"
#target_path = "C:/Users/Future/Desktop/results"
#Path(dir_name) / output_folder / str(base_filename + suffix)
#subprocess.run(["magick", "mogrify", "-path", RESULTS_DIR, "-format", "PNG", IDs_DIR + "/*tiff"])
subprocess.run(["magick", "mogrify", "-path", RESULTS_DIR, "-format", "PNG", Path(IDs_DIR) / str("*tiff")])