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) 

I run python af.py -p "C: \ Program Files \ Internet Explorer" Mistake

    2 answers 2

    Try this:

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

    An excerpt from the documentation explaining why it is preferable to pass an argument list instead of a string (just your case) :

    args is a string, or a sequence of program arguments. For example, it is recommended that you use it. If you are passing through a single string, you must be able to see it.

    • I also want to ask. output2 = subprocess.check_output (['icacls', path, '/ grant: r', 'Administrators: F']). How to make Administrators appear in Cyrillic, not krakozybrami? - Alex Firsov
    • @AlexFirsov, in what encoding is the script stored? - MaxU
    • Saved to utf-8 - Alex Firsov
    • It’s probably better to ask a separate question with a reproducible example ... - MaxU
    • @AlexFirsov, I also advise you to look at this answer - MaxU

    The problem is not argparse. The path contains whitespace characters, so when it is passed directly to the shell, it is interpreted not as a single parameter, but as a set. Add screening or use the method specified by @MaxU