0

These are the 3 scripts I'm working on which are to be used in other various scripts, the 4th one is an example piece:

1

import os
data_dir = os.path.expandvars (r"C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\system32.lnk")

os.system(r"start data_dir")

2

import shutil
import os

data_dir = os.path.expandvars (r"C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data")

shutil.rmtree(r"data_dir")

3

import os
data_dir = os.path.expandvars (r"C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data\startme.exe")

os.system(r"terminate data_dir")

4

import os
data_dir = os.path.expandvars (r'C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\system32.lnk')

os.remove(data_dir)

The issue that I'm having is with scripts 1&2 are that saying that windows cannot find the file specified, Yet in script #4 I used the exact same method to identify where the item/file is located in that code and it seems to work without a hitch.

I've been trying to figure this out all morning and yes I've learned alot in the process but I'm feeling a bit stumped as to why I can't get script #1 and #2 working.

ps. I'm also have a small issue of lesser importance, in the 3rd script I posted its saying "'terminate' is not recognized as an internal or external command,operable program or batch file."

Any help would be greatly appreciated, Thanks!!

Edit,

import subprocess
import os

data_dir = os.path.expandvars ("C:\\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data\\startme.exe")

subprocess.call(["start", data_dir])
Rejnaps
  • 113
  • 2
  • 12
  • I dont know if it is a typo, but in method 1 you have `\\system32.lnk` and `C:\\Users` in method 4 you have `\systeam32.lnk` and `C:\Users` – Elias May 13 '14 at 23:44

1 Answers1

1

The issue in all of your examples is that you're putting the variable name data_dir within a quoted string. Python doesn't fill in the variable's contents in the string, which you seem to be expecting, it puts the literal text "data_dir". Since you don't have a file named "data_dir", you get various exceptions.

The solution for case #2 is very simple, just get rid of the quotes:

check = shutil.rmtree(data_dir)

For #1 and #3, it's a bit more complicated, since you need to pass the directory name to another process. One option is to use string formatting:

os.system("start {}".format(data_dir))

That may not work right though, if you have spaces or other unexpected characters in the data_dir string. A safer approach may be to add quotation marks within the string:

os.system('start "{}"'.format(data_dir))

Or, you could use a better system for calling an external command, like subprocess:

import subprocess
subprocess.call(["start", data_dir])

The issue with #3 saying that terminate is not a command is not Python related. There's no terminate command in the default path of my version of Windows, and I'm not aware of any command by that name stashed somewhere out of the way. If you expect there to be a command by that name on your system, you may need to edit your system path, or pass a full path, rather than just the program name.

Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • Ok so i tested #1 with what you told me and it worked, #2 didn't work with the first method **(it complained about the whitespace)** the second method had no errors but pulled up 1 cmd quickly and then another one that was set to the path of the files folder oddly enough and the third method says it can't find the folder and point to the SubProcess file in WingIDE debugger. http://i.imgur.com/vXOyMti.png – Rejnaps May 14 '14 at 00:05
  • Also how would you go about Terminating a process ? aka Ending a Task – Rejnaps May 14 '14 at 00:13