0
import argparse
import ctypes
import subprocess
parser = argparse.ArgumentParser(description='test')
parser.add_argument('-p', help = 'folder')
args = parser.parse_args()
path = args.p
if ctypes.windll.shell32.IsUserAnAdmin() != 0:
    print 'You admin\n'
    subprocess.call('takeown /f' + ' '  + path + ' ' + '/R  /A')
    print 'done'
else:
    print 'You not  admin'
    exit(0)

Запускаю так python af.py -p "C:\Program Files\Internet Explorer" Ошибка

2 ответа 2

4

Попробуйте так:

output = subprocess.check_output(['takeown', '/F', path, '/R', '/A'])

Выдержка из документации, поясняющая почему предпочтительнее передавать список аргументов вместо строки (как раз ваш случай):

args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

5
  • Еще хочу спросить. output2 = subprocess.check_output(['icacls', path, '/grant:r', 'Администраторы:F']). Как сделать так чтобы Администраторы отображался кириллицой ,а не кракозябрами? 25 мая 2018 в 9:51
  • @AlexFirsov, в какой кодировке сохранен скрипт? 25 мая 2018 в 9:54
  • Сохранен в utf-8 25 мая 2018 в 9:55
  • лучше наверное задать отдельный вопрос с воспроизводимым примером... 25 мая 2018 в 9:59
  • @AlexFirsov, также советую посмотреть этот ответ 25 мая 2018 в 10:00
1

Проблема не в argparse. Путь содержит пробельные символы, поэтому при прямой передаче в shell интерпретируется не как один параметр, а как набор. Добавьте экранирование или используйте способ, указанный @MaxU

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.