Python Forum
Why is subprocess.call command not working?
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is subprocess.call command not working?
#1
In the code snippet below, the subprocess.call command generates the file not found error shown below. However, passing the same variable "command" to os.system works. I've read that subprocess is preferroed over os.system, so I'd like to get that working.

-Thanks


for file in jpgfiles:
    jpgfile = '"' + jpgdir + file + '"'
      
    command = "convert " + str(jpgfile) + " -font Helvetica -pointsize 60 -fill lightblue -quality 100 -gravity southeast -auto-orient -annota
  
    # Generates file not found error shown below
    # subprocess.call([command])

    # WORKS
    os.system(command)
Error:
FileNotFoundError: [Errno 2] No such file or directory: 'convert "/home/bfbarton/CentOS Share/pics/Trenton Makes Bridge-10062018.jpg" -font Helvetica -pointsize 60 -fill lightblue -quality 100 -gravity southeast -auto-orient -annotate +80+50 %[exif:DateTimeOriginal] "/home/bfbarton/CentOS Share/pics/Trenton Makes Bridge-10062018.jpg"': 'convert "/home/bfbarton/CentOS Share/pics/Trenton Makes Bridge-10062018.jpg" -font Helvetica -pointsize 60 -fill lightblue -quality 100 -gravity southeast -auto-orient -annotate +80+50 %[exif:DateTimeOriginal] "/home/bfbarton/CentOS Share/pics/Trenton Makes Bridge-10062018.jpg"'
Reply
#2
For subprocess.call, the first argument is the command you're calling, with args in subsequent elements. In this case, the first element should only be "convert". Args are then passed to it, and will be escaped/quoted as needed for you.

From the docs:
>>> import subprocess
>>> help(subprocess.call)
Help on function call in module subprocess:

call(*popenargs, timeout=None, **kwargs)
    Run command with arguments.  Wait for command to complete or
    timeout, then return the returncode attribute.

    The arguments are the same as for the Popen constructor.  Example:

    retcode = call(["ls", "-l"])

>>>
Reply
#3
Line 4, 3 quotes.
You have to break the command by spaces.

command = "convert " + str(jpgfile) + " -font Helvetica -pointsize 60 -fill lightblue -quality 100 -gravity southeast -auto-orient -annota"
command = command.split()
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
(Nov-19-2018, 09:02 PM)wavic Wrote: Line 4, 3 quotes.
You have to break the command by spaces.

command = "convert " + str(jpgfile) + " -font Helvetica -pointsize 60 -fill lightblue -quality 100 -gravity southeast -auto-orient -annota"
command = command.split()


Now I'm getting the error below.

-Thanks

jpgdir = "/home/bernie/CentOS Share/pics/"
jpgfiles = fnmatch.filter(os.listdir(jpgdir), '*.jpg')

command = "convert " + str(jpgfile) + " -font Helvetica -pointsize 60 -fill lightblue -quality 100 -gravity southeast -auto-orient -annotate +80+50 %[exif:DateTimeOriginal] " + str(jpgfile)
    command = command.split()

    subprocess.call([command])
Error:
Traceback (most recent call last): File "./im-datestamp.py", line 36, in <module> subprocess.call([command]) File "/opt/rh/rh-python36/root/usr/lib64/python3.6/subprocess.py", line 267, in call with Popen(*popenargs, **kwargs) as p: File "/opt/rh/rh-python36/root/usr/lib64/python3.6/subprocess.py", line 709, in __init__ restore_signals, start_new_session) File "/opt/rh/rh-python36/root/usr/lib64/python3.6/subprocess.py", line 1258, in _execute_child executable = os.fsencode(executable) File "/opt/rh/rh-python36/root/usr/lib64/python3.6/os.py", line 800, in fsencode filename = fspath(filename) # Does type-checking of `filename`. TypeError: expected str, bytes or os.PathLike object, not list
Reply
#5
command is already a list.
Why do you put it in square brackets?

Fix the indentation.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
@zBernie as posted over is best to past in a list,can try with string first then need shell=True.
Can make a list later,as you use 3.6 i have put in some updates.
import subprocess
import os

jpgdir = "/home/bernie/CentOS Share/pics/"
file = 'test.jpg'
jpgfile = os.path.join(jpgdir, file)
command = f'convert "{jpgfile}" -font Helvetica -pointsize 60 -fill lightblue -quality 100 -gravity southeast -auto-orient -annotate +80+50 %[exif:DateTimeOriginal] {jpgfile}'
subprocess.run(command, shell=True)
Dos not work post your original convert command before you take into Python.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 630 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Using subprocess to execute complex command with many arguments medatib531 5 1,717 Apr-27-2023, 02:23 PM
Last Post: medatib531
  use subprocess on linux\pi wwith a "grep " command korenron 2 7,901 Oct-19-2021, 10:52 AM
Last Post: DeaD_EyE
  continue if 'subprocess.call' failes tester_V 11 5,007 Aug-26-2021, 12:16 AM
Last Post: tester_V
  printing out the contents aftre subprocess.call() Rakshan 3 2,701 Jul-30-2021, 08:27 AM
Last Post: DeaD_EyE
  Subprocess.Popen() not working when reading file path from csv file herwin 13 14,617 May-07-2021, 03:26 PM
Last Post: herwin
  subprocess call cannot find the file specified RRR 6 16,382 Oct-15-2020, 11:29 AM
Last Post: RRR
  Error when running mktorrent subprocess command pythonnewbie138 4 3,764 Sep-16-2020, 01:55 AM
Last Post: pythonnewbie138
  Select correct item from list for subprocess command pythonnewbie138 6 3,210 Jul-24-2020, 09:09 PM
Last Post: pythonnewbie138
  Why wont subprocess call work? steve_shambles 3 2,604 Apr-28-2020, 03:06 PM
Last Post: steve_shambles

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020