Question

I have a program that processes files and returns another file as output. When I am running it in cmd I first set the path: "cd c:\program" and then set it to process the file located in the program folder: "program test.txt". I would like a python program to do it for me using the subprocess module, but I can't get it to work.

I have read the related posts and I know it should be a no-brainer, but as a novice I haven't been able to figure it out. Help greatly appriciated.

Here is one example of the code I tried. It runs, but doesn't produce any results.

import subprocess

textfile = 'c:\program\test.txt'
programPath = r'C:\program\program.exe'
subprocess.Popen([programPath, textfile])
Était-ce utile?

La solution

You forgot to prepend r to textfile's literal:

textfile = r'c:\program\test.txt'

(\t is a tab character. Next time, please include any error messages in the post as well.)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top