0
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

asked May 7, 2022 at 22:04
3
  • Hey. Can you add in the contents of the ids.txt file to your question? (or the basic gist of it).. (Just going to test it here) Commented May 7, 2022 at 22:16
  • 1
    Hey sara the file is empty Commented May 7, 2022 at 22:19
  • 2
    for line in ids: loops through the lines in the file and assigns each line to line. If the file is empty the loop does nothing. That is why the for statement appears not to work. It's not getting any data to work with. Commented May 7, 2022 at 22:39

2 Answers 2

1

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
answered May 7, 2022 at 22:55
Sign up to request clarification or add additional context in comments.

Comments

0

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()
answered May 7, 2022 at 22:22

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.