2
import os
import time
import shutil
import subprocess
data_dir = os.path.expandvars ('C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data\Startup\delete.txt')
s=0
m=0
h=0
while s<=60:
 os.system('cls')
 print h, 'Hours', m, 'Minutes', s, 'Seconds'
 time.sleep(1)
 s+=1
 if s == 60:
 m+=1
 s=0
 elif m == 60:
 h+=1
 m=0
 s=0
 if s == 2:
 os.system("TASKKILL /F /IM chrome.exe")
 os.system("start textfile.txt")
 if s == 5:
 os.remove(data_dir)
 if s == 10:
 shutil.rmtree('C:\Users\SpanjerX\Downloads\Zips\Z.Current Project\deleteme')

Hey I'm semi new to coding and I really can't figure out why my environmental variables won't work in this script if I do a Print text test my user name comes out in the path but in this particular script I can't seem to get os.remove command to work in conjunction with the %USERNAME% environmental variable.

Any help would be greatly appreciated :)

Edit, I'm trying to get this portion of the code:

import os
import time
import shutil 
import subprocess
data_dir = os.path.expandvars ('C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data\Startup\delete.txt')
if s == 5:
 os.remove(data_dir)

to delete this file "('C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data\Startup\delete.txt')"

but the Variable %USERNAME% doesn't seem to be working in this case, i'm not 100% sure why.

Edit 2, K I got it working the issue I was have was something called fatigue :P, i was doing it right but I wasn't being wary enough if path names were correct -_- O_O. But non the less ChrisProsser gave me a respectable answer and i commend him for dealing with the sleepy Spanjer.

Hey BTW thanks alot for the help and sorry my question was so...lets say messy :P I figured I had enough energy to make my question but I think I wasn't 100% there when I asked the question. Thanks Alot !

Respectfully and with great Honor, Spanjer

asked May 12, 2014 at 12:05
2
  • 1
    I'm not entirely clear which part you are stuck on. Is it how to get a path to a file into your variable that is based on an environment variable? Commented May 12, 2014 at 12:13
  • I add'd a few edits above, Ty for the help Commented May 12, 2014 at 12:20

1 Answer 1

0

I have reproduced a simplified version of your program and it seems to work fine:

import os
data_dir = os.path.expandvars ('C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data\Startup\delete.txt')
print(data_dir)
os.remove(data_dir)

I would suggest checking the printout of your path produced by the above carefully against where the file is actually located.

The print returns the output below showing the environment variable was interpreted correctly:

C:\Users\Prosserc\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Data\Startup\delete.txt

If the file is there it goes ahead and deletes it, if not it raises the error:

WindowsError: [Error 2] The system cannot find the file specified: 'C:\\Users\\Prosserc\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Data\\Startup\\delete.txt'

Again the path quoted in the error message should confirm whether the environment variable was interpreted correctly.

answered May 12, 2014 at 12:32
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.