我一直在尝试学习 python 装饰器并编写了以下代码。但它不断给出错误。
import os
import sys
def authenticate(fn):
def wrapper(*args):
euid = os.geteuid()
if euid != 0:
print "This command requires privliged mode. Enter password.."
args = ['sudo', sys.executable] + sys.argv + [os.environ]
os.execlpe('sudo', *args)
fn(*args)
return wrapper
@authenticate
def show_files(dir):
files = [f for f in os.listdir(dir)]
for file in files:
if os.path.isfile(file):
print file
dir = raw_input("Please enter a directory name\t")
show_files(dir)
我得到以下回溯:
Traceback (most recent call last):
File "auth1.py", line 24, in show_files(dir)
File "auth1.py", line 11, in wrapper fn(*args)
File "auth1.py", line 16, in show_files files = [f for f in os.listdir(dir)]
OSError: [Errno 2] No such file or directory: ''
这个想法是show_files
只对可以验证自己的用户运行该功能。我的问题似乎是身份验证的编写方式和execlpe
调用方式。任何帮助表示赞赏。