I am trying to run a batch file through python; however, it is not recognizing the path. It stops reading the path after the space between 'Practice' and 'Folder'. How can I fix this? I've tried the r and using forward and backward slashes. Any help would be awesome. Thank you!
import osPractice = r"C:\Users\Username\Desktop\Practice Folder\Practice.bat"os.system(Practice)'C:\Users\Username\Desktop\Practice' is not recognized as an internal or external command, operable program or batch file.
4 Answers4
Try usingcall fromsubprocess module.You need to enclose the command only in double quotes.
from subprocess import callcall(r'"C:\Users\Username\Desktop\Practice Folder\Practice.bat"')(Notice the order of placing quotes...)
This would even work withos.system() provided you take care the order of quotation marks.
from os import systemsystem(r'"C:\Users\Username\Desktop\Practice Folder\Practice.bat"')This should help fix your problem.
Comments
Change working directory to the script directory as you are using some relative redirection paths. Pushd changes current directory to any drive and can map network drives. The&& chains commands and only runs the right hand command if the left hand command succeeds.%UserProfile% is a standard environmental variable which is usually better then using a fixed path ofC:\Users\Username.
import osPractice = r'pushd "%UserProfile%\Desktop\Practice Folder" && Practice.bat'os.system(Practice)1 Comment
You probably need to use two types of quotation marks e.g.
import osPractice = r"'C:\Users\Username\Desktop\Practice Folder\Practice.bat'"os.system(Practice)As it is, your string does not contain quotation marks - you need to include quotation marks within your string or else Windows will think thatFolder\Practice.bat is an argument to the command rather than a continuation of the file path
1 Comment
Try this
import osPractice = os.path.abspath(r"C:\Users\Username\Desktop\Practice Folder\Practice.bat")Edit:
Something like this worked for me
os.system(r'"C:\Users\Username\Desktop\Practice Folder\Practice.bat"')8 Comments
"C:\Users\Username\Desktop\Practice Folder\Practice.bat" into the command line?Explore related questions
See similar questions with these tags.
