0

Currently, this code works. It creates a text file named SerialNumber that saves in pictures. The problem is I can't seem to work out how to get it to work on any computer. So for example, if I remove \users\Jarvis from the file path it no longer finds its way to pictures. I'm trying to get it to work no matter who is logged in.

import os


Save_Path='C:\\Users\\Jarvis\\Pictures\\SerialNumber.txt'

with open(Save_Path, 'w') as f:
    f.write(str(os.system('start cmd /k "wmic bios get serialnumber >C:\\Users\\Jarvis\\Pictures\\SerialNumber.txt"')))

I've tried to set it as: \users\Admin \users%UserProfile%
\users\user but that returns FileNotFoundError: [Errno 2] No such file or directory:


import os
from pathlib import Path

Save_Path= 'C:\\Users\\Pictures\\SerialNumber.txt'

path = Path.home() / "Pictures\SerialNumber.txt"

with open(path, 'w') as f:
    f.write(str(os.system('start cmd /k "wmic bios get serialnumber >C:\\Users\\%UserProfile%\\Pictures\\SerialNumber.txt"')))

With Subprocess and replace I was able to print to python just the serial number

import subprocess
SerialNumber = 'wmic bios get serialnumber'
result = subprocess.getoutput(SerialNumber)
print(result.replace("SerialNumber", ""))
  • I tried using print os.path.expanduser("\pictures\SerialNumber.txt") that didn't work either I assume it's written correctly –  Aug 19 '22 at 14:44
  • 1
    Does this answer your question? [platform-independent way to fetch pictures folder?](https://stackoverflow.com/questions/26618844/platform-independent-way-to-fetch-pictures-folder) – ChoKaPeek Aug 19 '22 at 14:55
  • @ChoKaPeek I tried the selected answer but may just not be adding it in the right way it's telling me permission denied: 'C:\users\Jarvis\pictures Line 8 –  Aug 19 '22 at 15:20
  • your new path does not contain the name of your file: try "Pictures\\SerialNumber.txt" instead of "Pictures" – ChoKaPeek Aug 19 '22 at 15:22
  • @ChoKaPeek that runs python with no errors, creates the file in pictures. In Cmd it returns The filename, directory name, or volume label syntax is incorrect. and the file itself is only putting 0 where (when I dont define the file path and it defaults to desktop it puts the serialnumber). I also changed line 9 to > Path that got rid of the CMD error but still writes 0 into the file. –  Aug 19 '22 at 15:28
  • - %UserProfile% contains an absolute path, it does not need C:\\Users\\ in front of it. - why 0? because it is the [return value of os.system](https://docs.python.org/fr/3/library/os.html#os.system) – ChoKaPeek Aug 19 '22 at 15:40
  • You either want to [handle the output](https://stackoverflow.com/questions/18739239/python-how-to-get-stdout-after-running-os-system) in python or just make the process write to the file by redirection, in which case you don't need to open() or write() the file yourself. Here you are doing both at the same time – ChoKaPeek Aug 19 '22 at 15:46
  • @ChoKaPeek Honestly I wasn't sure how to get the output from cmd so I put the output into a file. As the code improves it will eventually enter the serial number into the dell.com/support website's search bar. (the goal is to automate downloading a computer-specific driver over potentially dozens of machines where it won't have the same user) –  Aug 19 '22 at 15:52
  • @ChoKaPeek So this works but How do I grab the serial number to input in the search bar for the next step? ``` python import subprocess SerialNumber = 'wmic bios get serialnumber' result = subprocess.getoutput(SerialNumber) print(result) ``` –  Aug 19 '22 at 15:59
  • Although this doesn't modify the output, as an extension of the idea I offered, _though ignored_, in your last question, what about: ```import os```, ```import subprocess```, ```home_dir = os.path.expanduser( '~' )```, ```sn_file = os.path.join( home_dir, 'Pictures', 'SerialNumber.txt' )```, ```subprocess.Call( 'wmic.exe /Output:sn_file BIOS Get SerialNumber' )```. As you can see, there is still no need at all to directly involve `cmd exe` in your code. – Compo Aug 20 '22 at 19:15
  • @Compo I tried to create a room to talk with you tbh idk if I did it right. in the final attempts of the code I tweaked it so it uses subprocess and outputs to python instead of cmd. It doesn't need a file path I found out that was irrelevant to do since it was outputting both to python and cmd. I'd be happy to speak with you about where that particular code stands. however, I figured out that using Selenium may be a better option altogether. instead of mouse clicks (this part wasn't relevant to the earlier questions of this project) Again I appreciate your assistance moving forward –  Aug 20 '22 at 19:57

1 Answers1

0

Okay after some help, and research, This is the final code. It uses a subprocess that will output to python>CMD. I then used re (lines 7 and 8) to .strip and re.sub removing everything that wasn't actually the serial number. I installed pyperclip to copy

import subprocess
import pyperclip
import re
import os

SerialNumber = 'wmic bios get serialnumber'
result = subprocess.getoutput(SerialNumber)
SerialResult = (result.strip("SerialNumber"))
print(re.sub("[^a-zA-Z0-9]+", "", SerialResult))
pyperclip.copy(re.sub("[^a-zA-Z0-9]+", "", SerialResult))