Python Forum
subprocess for executing over CLI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
subprocess for executing over CLI
#1
Hi,

this is my first attempt to execute something via CLI using python - I'm totally unexperienced with "subprocesses"...

I'm trying to execute a concatenation of 2 pngs into 1 pdf via python using the commandline.
Therefore I'm using naps2.

Executing it manually it works...

Here is the example:
C:\Program Files\NAPS2>naps2.console -i "D:\Daten\aktuell\test\1.png;D:\Daten\aktuell\test\2.png" -n 0 --output "D:\Daten\aktuell\test\gut.pdf"
But when launching over cli it is said "An unexpected error occurs."

Here is the python code:

import subprocess

subprocess.run(["C://Program Files//NAPS2//naps2.console.exe", "-i", '"D://Daten//aktuell//test//1.png;D://Daten//aktuell//test//2.png"', "-n", "0", "--output", '"D://Daten//aktuell//test//gut3.pdf"'], shell=False)
I read online that you have to pass the arguments as single strings (shell=false).

https://stackoverflow.com/questions/1167...-arguments

I'm trying to avoid "shell=True" because of security problems, which could occur.

What can I do?

It would be great, if you could help me out...

Thanks...
Reply
#2
"C://Program Files//NAPS2//naps2.console.exe"

That double slash seems strange to me.
Reply
#3
(Jul-08-2023, 06:14 PM)Axel_Erfurt Wrote: "C://Program Files//NAPS2//naps2.console.exe"

That double slash seems strange to me.
Hi,
thanks for answering!!

Here the same problem occurs...

subprocess.run([r"C:\Program Files\NAPS2\naps2.console.exe", "-i", r'"D:\Daten\aktuell\test\1.png;D:\Daten\aktuell\test\2.png"', "-n", "0", "--output", r'"D:\Daten\aktuell\test\gut3.pdf"'], shell=False)
Reply
#4
Hi,

I got it working!!

import subprocess

subprocess.run(['C:\\Program Files\\NAPS2\\naps2.console.exe','-i', 'D:\\Daten\\aktuell\\test\\1.png;D:\\Daten\\aktuell\\test\\2.png','-n','0','--output','D:\\Daten\\aktuell\\test\\gut4.pdf'], shell=False)
this works too:
import subprocess

subprocess.run(['C:/Program Files/NAPS2/naps2.console.exe','-i', 'D:/Daten/aktuell/test/1.png;D:/Daten/aktuell/test/2.png','-n','0','--output','D:/Daten/aktuell/test/gut7.pdf'], shell=False)
Thanks for answering...

Greetings...
Reply
#5
You can also use a raw string.
path = r'C:\foldr\file.ext'
I prefer using "/" for file paths. Save "\" for escape sequences and regular expressions.
Reply
#6
Hi deanhystad,

I will prefer using / for paths too - it is the easiest way.
I used \\ to avoid \ being recognized as an escape sequence.
Thanks for the information about raw strings.

Have a nice day!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parallel executing a command using subprocess? larkypython 0 2,175 Nov-28-2019, 03:49 PM
Last Post: larkypython

Forum Jump:

User Panel Messages

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