如何在python中删除元素并更改特定元素?

时间:2019-04-23 03:46:44

标签: python-2.7 tkinter

我正在编写一个Python程序来管理FreeRadius用户帐户。我可以在程序中添加新用户,但是在输入目标用户的用户名和密码后无法删除用户。另外,我无法更改用户密码。

我应该如何更改编码以删除用户并提供密码更改功能?

npm cache clear --force

预期输出:

  1. 用户已删除
  2. 用户密码已更改

实际输出:无效的语法

1 个答案:

答案 0 :(得分:0)

您的代码存在以下语法问题:

  • 没有适当的缩进。当您在问题中发布代码时,它们可能已被删除。无论如何,请检查它们;
  • 您的with语句在末尾缺少as fh:

还有其他问题:

  • 删除或更改配置文件时,应打开该文件进行写入,而不是追加,因为您将重新创建它。因此参数应为w;
  • 可能存在错误的删除或更改,因为您检查整个配置行中的用户名或密码,而不是解析它并检查确切的字段。如果某个用户的密码与另一个用户的名称相同,则在删除另一个用户时,将同时删除这两个用户。我没有修复这部分代码,请留给您

未经测试的更正代码如下:


#!usr/bin/python
from Tkinter import *
import ttk
import os
import subprocess
import commands

main = Tk()

main.title("FreeRadius Windows Administration Controller GUI")
main.geometry('1280x800')

rows = 0
while rows < 50:
    main.rowconfigure(rows, weight=1)
    main.columnconfigure(rows, weight=1)
    rows += 1

def startlog():
    f = open('C:/FreeRADIUS.net/var/log/radius/radius.log', 'r')
    file_contents = f.read()
    txtarea_output.insert(INSERT, file_contents)
    print (file_contents)
    f.close()

def resetlog():
    txtarea_output.delete(1.0,END)

def adduser():
    fh = open('C:/FreeRADIUS.net/etc/raddb/users.conf', 'a')
    fh.write("\n")
    fh.write(createuserentry.get() + "     " + "User-Password ==  " + '"' +   createuserpwd.get() + '"')
    fh.close()

def deleteuser():
    with open('C:/FreeRADIUS.net/etc/raddb/users.conf', "r") as fh:
        lines = fh.readlines()
    with open('C:/FreeRADIUS.net/etc/raddb/users.conf', "w") as fh:
        for line in lines:
            if deleteuserentry.get() in line: continue
            fh.write(line)

def cpwd():
    with open('C:/FreeRADIUS.net/etc/raddb/users.conf', "r") as fh:
        lines = fh.readlines()
    with open('C:/FreeRADIUS.net/etc/raddb/users.conf', "w") as fh:
        for line in lines:
            if changeuserpwd.get() in line:
                fh.write(changepwduserentry.get() + "     " + "User-Password ==  " + '"' +   changepwdusernewpwd.get() + '"')
            else:
                fh.write(line)

nb = ttk.Notebook(main)
nb.grid(row=1, column=0, columnspan=40, rowspan=39, sticky='NESW')

page1 = ttk.Frame(nb)
nb.add(page1, text='User Account Management')
createuser = ttk.Label(page1, text="Create User").grid(column=0, row=0,     sticky='W')
createusername = ttk.Label(page1, text="Enter new user's   username:").grid(column=0, row=1, sticky='W')
createuserentry = ttk.Entry(page1)
createuserentry.grid(row=1, column=1, columnspan=2)
createuserpassword = ttk.Label(page1, text="Enter new user's password:").grid(column=0, row=2, sticky='W')
createuserpwd = ttk.Entry(page1)
createuserpwd.grid(row=2, column=1, columnspan=2)
createuserconfirm = Button(page1, text='Create this User', borderwidth=2, command=adduser)
createuserconfirm.grid(row=3, column=0, sticky='W')

ttk.Label(page1, text="").grid(column=0, row=4, sticky='W')

deleteteuser = ttk.Label(page1, text="Delete User").grid(column=0, row=5, sticky='W')
deleteusername = ttk.Label(page1, text="Enter current user's username:").grid(column=0, row=6, sticky='W')
deleteuserentry = ttk.Entry(page1)
deleteuserentry.grid(row=6, column=1, columnspan=2)
deleteuserpassword = ttk.Label(page1, text="Enter current user's password:").grid(column=0, row=7, sticky='W')
deleteuserpwd = ttk.Entry(page1)
deleteuserpwd.grid(row=7, column=1, columnspan=2)
deleteuserconfirm = Button(page1, text='Deletete this User', borderwidth=2, command=deleteuser)
deleteuserconfirm.grid(row=8, column=0, sticky='W')

ttk.Label(page1, text="").grid(column=0, row=9, sticky='W')

changepwd = ttk.Label(page1, text="Change User's password").grid(column=0, row=10, sticky='W')
changepwdusername = ttk.Label(page1, text="Enter user's username:").grid(column=0, row=11, sticky='W')
changepwduserentry = ttk.Entry(page1)
changepwduserentry.grid(row=11, column=1, columnspan=2)
changepwduserpassword = ttk.Label(page1, text="Enter user's current password:").grid(column=0, row=12, sticky='W')
changepwduserpwd = ttk.Entry(page1)
changepwduserpwd.grid(row=12, column=1, columnspan=2)
changepwdusernewpwd = ttk.Label(page1, text="Enter user's new password:").grid(column=0, row=13, sticky='W')
changepwdusernewpwd = ttk.Entry(page1)
changepwdusernewpwd.grid(row=13, column=1, columnspan=2)
changepwduserconfirm = Button(page1, text='Change Password',   borderwidth=2, command = cpwd)
changepwduserconfirm.grid(row=14, column=0, sticky='W')