爲了改變位於文件「C:」你必須擁有管理員權限, 你
#!python
# coding: utf-8
import sys
import ctypes
def run_as_admin(argv=None, debug=False):
shell32 = ctypes.windll.shell32
if argv is None and shell32.IsUserAnAdmin():
return True
if argv is None:
argv = sys.argv
if hasattr(sys, '_MEIPASS'):
# Support pyinstaller wrapped program.
arguments = map(unicode, argv[1:])
else:
arguments = map(unicode, argv)
argument_line = u' '.join(arguments)
executable = unicode(sys.executable)
if debug:
print 'Command line: ', executable, argument_line
ret = shell32.ShellExecuteW(None, u"runas", executable, argument_line, None, 1)
if int(ret) <= 32:
return False
return None
if __name__ == '__main__':
ret = run_as_admin()
if ret is True:
print 'I have admin privilege.'
raw_input('Press ENTER to exit.')
elif ret is None:
print 'I am elevating to admin privilege.'
raw_input('Press ENTER to exit.')
else:
print 'Error(ret=%d): cannot elevate privilege.' % (ret,)
代碼摘自:啓動腳本或而這樣做,例如前可以讓他們How to run python script with elevated privilege on windows
腳本作者:Gary Lee
有人在某處使用該文件。也許這就是你運行的文件? – zmbq 2013-04-24 05:21:53
沒有。我認爲。我已經檢查過了。 – 2013-04-24 05:25:15
關閉所有程序和cmd提示窗口,然後再試一次,有東西有文件鎖定。 – Ardesco 2013-04-24 05:26:28