Reputation: 15
import random
import subprocess
current_machine_id = str(subprocess.check_output('wmic csproduct get uuid'), 'utf-8').split('\n')[1].strip()
ids = open('ids.txt','r')
for line in ids:
if line == current_machine_id:
print("STAGE 1 TRIGGERD") #remove once dev stage is done
print(f"your machine is already regetired your hwid is {line}")
else:
print("STAGE 2 TRIGGERD")#remove once dev stage is done
print("your id was not found")
with open('ids.txt','r+') as file:
file.write(current_machine_id)
exit
what it should do is get the hwid and check if its regesitrd in the txt file or not if it is then it should inform you of your hwid and if its not it should write your hwid in the txt file but for some reason the whole for statment doesnt work
Upvotes: 0
Views: 101
Reputation: 74
You have not considered the case where the txt file is empty that's why the for does not work, also I always recommend opening the files with the with construct it is much safer, in addition you forgot to open the file in append mode (I assume you want to add id at the end and not have a file with a single id)
a: Opens a file for appending new information to it. The pointer is placed at the end of the file. A new file is created if one with the same name doesn't exist.
r+: Opens a file for reading and writing, placing the pointer at the beginning of the file.
import random
import subprocess
current_machine_id = str(subprocess.check_output('wmic csproduct get uuid'), 'utf-8').split('\n')[1].strip()
with open('ids.txt','r') as ids_f:
ids=[id.strip() for id in ids_f.readlines()] #remove /n
if current_machine_id in ids:
print("STAGE 1 TRIGGERD") #remove once dev stage is done
print(f"your machine is already regetired your hwid is {current_machine_id}")
else:
print("STAGE 2 TRIGGERD")#remove once dev stage is done
print("your id was not found")
with open('ids.txt','a') as file: # open file in append
file.write(current_machine_id+"\n") # add in new line
exit
Upvotes: 1
Reputation: 132
Do you know if 'current_machine_id' is being set to anything?
Heres the documentation for subprocess.check_output
https://docs.python.org/3/library/subprocess.html#subprocess.check_output
Take the long line and break it down into each function call untill you can determine where exactly it is having an issue
Try just getting a return value and printing it. I wouldn't worry about the files for now, they are just complicating the workspace.
Edit:
I pulled one function call out for you. Keep simplifying it, and look up the function on the documentation website
subprocess_result = subprocess.check_output('wmic csproduct get uuid')
print(subprocess_result)
current_machine_id = str(subprocess_result, 'utf-8').split('\n')[1].strip()
Upvotes: 0