2

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.

askedJan 17, 2018 at 15:31
J. D.'s user avatar
0

4 Answers4

2

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.

answeredJan 18, 2018 at 16:55
Melvin Abraham's user avatar
Sign up to request clarification or add additional context in comments.

Comments

1

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)
answeredJan 18, 2018 at 16:27
michael_heath's user avatar

1 Comment

Worked exactly the way i needed! Thank you!
0

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

answeredJan 17, 2018 at 15:32
Jonathon McMurray's user avatar

1 Comment

This gives the "The filename, directory name, or volume label syntax is incorrect." error.
0

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"')
answeredJan 17, 2018 at 15:34
SuperStew's user avatar

8 Comments

Does it work if you copy/paste"C:\Users\Username\Desktop\Practice Folder\Practice.bat" into the command line?
It does not, it gives the error "'C:\Users\Username\Desktop\Practice' is not recognized as an internal or external command, operable program or batch file."
Are you including the quotations marks? Because they're important
Ahhhh, it works. Your code here also works now: os.system(r'"C:\Users\Username\Desktop\Practice Folder\Practice.bat"') But now I have to figure out why it is not executing the whole batch file. It executes everything when I double click on the batch file but does partial when executing from cmd and python. Giving me the error "The system cannot find the path specified." from the batch file now.
Cool. If that gets you what you want, go ahead and accept the answer to close the question.
|

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.