-1

I'm having a problem with Python programming. In my program I use a os.startfile() to start a program but it won't let me.

When my program is executing the command to start the file I get the following error in the Windows Script Host:

Script:     C:\Users\Personal Name\Desktop\Program\Files\Volume\Volume Max\Volume Max.vbs

Line: 3

Token: 1

Error: Can't find the file.

Code: 80070002

Source (null)

I have the file exactly where the path goes and the name is spelled correctly. I've double checked. I removed the spaces of the file name and I've looked up the error code I got with no success because Windows have the same error for other Windows files.

Now I'm maybe out way wrong and this is not a Python error but I think so.

Code:

import socket
import time
import os

host = '192.168.56.1'
port = 5000

clients = []

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
s.setblocking(0)

quitting = False
print ("Server Started")

while not quitting:
        try:
                data, addr = s.recvfrom(1024)
                if ("Quit") in str(data):
                    quitting = True

                print (time.ctime(time.time()) + str(addr) + str(data))

                if "Volume Max" in data:
                        print ("Volume Max")
                        os.startfile('C:\\Users\\wbe0719\\Desktop\\Program\\Files\\Volume\\Volume Max\\Volume Max.vbs')

        except:
                pass
s.close()
halfer
  • 19,824
  • 17
  • 99
  • 186
CoolJWB
  • 69
  • 1
  • 2
  • 10
  • 1
    How are you calling startfile exactly? – Padraic Cunningham Jul 03 '16 at 22:26
  • 1
    That looks like you're running a Visual Basic program, not a Python program... – John Gordon Jul 03 '16 at 22:27
  • Here's how I'm calling startfile: os.startfile('C:\Users\Personal Name\Desktop\Program\Files\Volume\Volume Max\Volume Max.vbs') – CoolJWB Jul 03 '16 at 22:30
  • Im not using Visual Basic. Though Im calling a Visual Basic Script. Hmm... It maybe that script im getting a error from. Hold on. Im going to go check some stuff. – CoolJWB Jul 03 '16 at 22:33
  • 2
    You would need `os.startfile(r'C:\Users\Personal Name\Desktop\Program\Files\Volume\Volume Max\Volume Max.vbs') ` i.e to prepend an r to make it a raw string or to replace the backslashes with forward slashes http://stackoverflow.com/questions/27384489/attributeerror-nonetype-object-has-no-attribute-ravel/27384641#27384641 – Padraic Cunningham Jul 03 '16 at 22:36
  • It seems to work on starting other files in the same folder but the .vbs script it won't let me start it thru Python and giving me the error that there's no such file directory. – CoolJWB Jul 03 '16 at 22:39
  • It didn't work. I just got the same error. Padraic. – CoolJWB Jul 03 '16 at 22:43
  • Can you post the actual Python program that you're running? (Did you really expect us to be able to help without it?) – John Gordon Jul 03 '16 at 22:46
  • On my way doing it right now. Sorry for letting you wait. – CoolJWB Jul 03 '16 at 22:48
  • `Program\\Files` is probably supposed to be `Program Files`. – John Gordon Jul 03 '16 at 23:03
  • Um. No. I call the files Program and then Files. I knew this would happend XD. – CoolJWB Jul 03 '16 at 23:14
  • I mean I call the folders Program and Files... – CoolJWB Jul 03 '16 at 23:25
  • What program produces the error block at the top of your question? It isn't coming from the Python script. – John Gordon Jul 03 '16 at 23:47
  • @JohnGordon, the error appears to be copied from a wscript.exe message box. Initially I thought the file not found error was due to the script not being found and that maybe there was a problem with the progid such as missing quotes around `%1`. But I missed that clearly there's a problem on line 3 of this .vbs script or another script that it's calling. Error 0x80070002 is a COM error code (0x80000000) for the Windows API (0x00070000) error code `ERROR_FILE_NOT_FOUND` (0x0002). – Eryk Sun Jul 04 '16 at 01:34
  • Well. Let me explain. I have a Python script that starts a .vbs script. The vbs script starts a .cmd file that sets the volume to the max via nircmd.exe. The .vbs script works if I start it without Python (manually) but when I run the Python Script to start the .vbs script the script gives me that error. Sorry for confusing everyone but Im really confused too. I use Python 2.7.12 and this is the .vbs script: Dim WinScriptHost Set WinScriptHost = CreateObject("WScript.Shell") WinScriptHost.Run Chr(34) & "Volume Max.bat" & Chr(34), 0 Set WinScriptHost = Nothing – CoolJWB Jul 04 '16 at 10:57
  • Try this instead `os.startfile('C:\\Users\\wbe0719\\Desktop\\Program\\Files\\Volume\\Volume Max\\Volume Max.vbs'.encode('string-escape'))` The path string needs to be a raw string. You can cast with .encode('string-escape') – SeF May 04 '17 at 12:57

2 Answers2

2

Backslashes in text can cause the next character to be interpreted as a special character instead of its actual value.

For example, \n is a newline character and \b is a backspace character.

There are three ways to fix this:

  1. Use forward slashes in your path instead of backslashes. DOS/Windows will still recognize them.

  2. Use a raw string, which treats the contents exactly as typed. To do this, prepend the letter 'r' to the string, like so:

    raw_path = r'\b'

  3. Escape each backslash with another backslash, like so:

    escaped_path = 'C:\\Users\\Personal Name\\Desktop\\...'

John Gordon
  • 29,573
  • 7
  • 33
  • 58
0

SOLVED:

I solved it!

Short way of explaining it: I moved the Volume Max.vbs file to the folder the python script is and just said open Volume Max.vbs script without any pathway in the os.startfile. Like this: os.startfile(Volume Max.vbs)

CoolJWB
  • 69
  • 1
  • 2
  • 10
  • If you start the Python script from another directory, e.g. in a command prompt or the run dialog, then the working directory won't be the Python script directory, and `startfile` will fail. The VBS script also inherits this working directory. If it needs to open a file, it should be able to find it relative to itself without relying on the working directory. You can hack this by first setting the working directory via `os.chdir(os.path.dirname(sys.argv[0]))`. But it would be better to use derived absolute paths. – Eryk Sun Jul 04 '16 at 14:08