Команда Power Shell в выводе python дает целое число. Зачем? - PullRequest
0 голосов
/ 30 мая 2020
import os
policyINAppconfigFile = os.system('%SYSTEMROOT%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -Command  "findstr "maxAllowedContentLength" C:\\Windows\\sysnative\\inetsrv\\config\\applicationHost.config "')
numbers = []
for word in policyINAppconfigFile.split():
    if word.isdigit():
        numbers.append(int(word))
print(numbers)

Приведенный выше код дает сообщение об ошибке:

Traceback (последний вызов последним): файл «c: / Users / Administrator / Documents / IIS code / testing123.py», строка 119, in for word в policyINAppconfigFile.split (): builtins.AttributeError: объект int не имеет атрибута split

1 Ответ

1 голос
/ 30 мая 2020

Когда вы запускаете команду через os.system, вы получаете код выхода обратно; не возвращаемое значение. Модуль подпроцесса отлично подходит для захвата вывода команды:


import subprocess
import os

power_shell = os.path.join(
    os.environ["SYSTEMROOT"], "System32",
    "WindowsPowerShell", "v1.0", "powershell.exe"
)

policyINAppconfigFile = subprocess.check_output(
    [power_shell, "-Command",  "findstr maxAllowedContentLength",
    "C:\\Windows\\sysnative\\inetsrv\\config\\applicationHost.config"]
)
...