Python Forum

Full Version: subprocess for executing over CLI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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...
"C://Program Files//NAPS2//naps2.console.exe"

That double slash seems strange to me.
(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)
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...
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.
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!