Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python和.bat文件获取计算机序列号_Python_Batch File_Serial Number - Fatal编程技术网

使用python和.bat文件获取计算机序列号

使用python和.bat文件获取计算机序列号,python,batch-file,serial-number,Python,Batch File,Serial Number,我最近一直在为考试而学习,我正在阅读的教科书告诉我,获取计算机序列号的命令(CMD)如下所示(作为降低盗版的一种方法) 我决定自己用python试试这个,我想得到我自己计算机的序列号,我创建了一个批处理文件(.bat),并将序列号记录到一个txt文件中,我想我可以用python读取,这会产生更多的问题,如果有什么问题的话。。。以下是我目前拥有的代码: SerialNumber.bat(顺便说一下,bat文件是新的) serial.txt的外观(稍加修改以不泄露我的序列号) Readtxt.py

我最近一直在为考试而学习,我正在阅读的教科书告诉我,获取计算机序列号的命令(CMD)如下所示(作为降低盗版的一种方法)

我决定自己用python试试这个,我想得到我自己计算机的序列号,我创建了一个批处理文件(.bat),并将序列号记录到一个txt文件中,我想我可以用python读取,这会产生更多的问题,如果有什么问题的话。。。以下是我目前拥有的代码:

SerialNumber.bat(顺便说一下,bat文件是新的)

serial.txt的外观(稍加修改以不泄露我的序列号)

Readtxt.py

# Trying to read in two different ways
with open("serial.txt", "r") as file:
    print(file.read())

lines = []
for line in open("serial.txt"):
    lines.append(line)
print(lines)
哪个输出:(不再显示完整图像)

问题是:

  • 如何将序列号存储为字符串
  • 为什么我用于阅读的每个不同方法都提供不同的输出

  • 感谢您的回答:)

    当重定向到文件时,wmic使用BOM写入UTF-16-LE

    > wmic bios get serialnumber >serial.txt
    
    > hex.exe serial.txt
    HEX:       +00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f  0123456789abcdef
    0000000000: FF FE 53 00 65 00 72 00  69 00 61 00 6C 00 4E 00  .■S.e.r.i.a.l.N.
    0000000010: 75 00 6D 00 62 00 65 00  72 00 20 00 20 00 0D 00  u.m.b.e.r. . ...
    
    > type readtext.py
    with open("serial.txt", 'rb') as file:
        print(file.read().decode('utf-16'))
    
    > py readtext.py
    SerialNumber
    5xxxxyyyy
    

    重定向到文件时,wmic使用BOM写入UTF-16-LE

    > wmic bios get serialnumber >serial.txt
    
    > hex.exe serial.txt
    HEX:       +00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f  0123456789abcdef
    0000000000: FF FE 53 00 65 00 72 00  69 00 61 00 6C 00 4E 00  .■S.e.r.i.a.l.N.
    0000000010: 75 00 6D 00 62 00 65 00  72 00 20 00 20 00 0D 00  u.m.b.e.r. . ...
    
    > type readtext.py
    with open("serial.txt", 'rb') as file:
        print(file.read().decode('utf-16'))
    
    > py readtext.py
    SerialNumber
    5xxxxyyyy
    

    看起来像unicode。。。。您可能需要对其进行解码。尝试使用“rb”而不是“r”进行读取,并在末尾添加一个类似unicode的
    .decode()
    。。。。您可能需要对其进行解码。尝试使用“rb”而不是“r”进行读取,并在末尾添加一个
    .decode()
    。。。
    > wmic bios get serialnumber >serial.txt
    
    > hex.exe serial.txt
    HEX:       +00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f  0123456789abcdef
    0000000000: FF FE 53 00 65 00 72 00  69 00 61 00 6C 00 4E 00  .■S.e.r.i.a.l.N.
    0000000010: 75 00 6D 00 62 00 65 00  72 00 20 00 20 00 0D 00  u.m.b.e.r. . ...
    
    > type readtext.py
    with open("serial.txt", 'rb') as file:
        print(file.read().decode('utf-16'))
    
    > py readtext.py
    SerialNumber
    5xxxxyyyy