在python中,如何检测输入输出文件之间的重复,防止在输出文件中添加重复项?

2024-10-05 12:28:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个python程序来管理FreeRadius中的用户帐户服务器。如果我不想在输出文件中添加任何重复的帐户 (用户.conf)。如何更改adduser命令(用于创建单个帐户)和au命令(用于导入文件以创建多个帐户)中的代码?你知道吗

用户名由行中第一个单词的5位数字组成。我不希望在创建用户帐户后在输出文件中重复它。你知道吗

  • 首选输出文件:
  • 81045用户密码==“81045”
  • 56811用户密码==“56811”
  • 56287用户密码==“56287”
  • 654用户密码==“65654”
  • 99737用户密码==“99737”
  • 56185用户密码==“56185”

  • 我不希望出现这种输出:

  • 81045用户密码==“81045”
  • 81045用户密码==“56811”
  • 654用户密码==“56287”
  • 654用户密码==“65654”
  • 99737用户密码==“99737”
  • 56185用户密码==“56185”

      def adduser():
      with open('C:/FreeRADIUS.net/etc/raddb/users.conf', "r+") as fh:
      completed_lines = set()
      for line in open('C:/FreeRADIUS.net/etc/raddb/users.conf', "r"):
        if createuserentry.get() and createuserpwd.set() not in completed_lines:
           fh.write('\n')
           fh.write(createuserentry.get() + "     " + "User-Password == " + '"' + createuserpwd.get() + '"')
           fh.write('\n')
           fh.close()
           tkMessageBox.showinfo("success", "Account created")
        else:
            fh.close()
            tkMessageBox.showinfo("Failure", "Username is duplicated")
    
    
    
    def au():
    au.filename = tkFileDialog.askopenfilename(initialdir = "/",title = "Select file to open",filetypes = (("txt","*.txt"),("all files","*.*")))
    print(au.filename)
    completed_lines_hash = set()
    with open('C:/FreeRADIUS.net/etc/raddb/users.conf', 'a') as output_file:
       with open(au.filename, "r") as input_file:
    for line in open(au.filename, "r"):
        hashValue = hashlib.md5(line.rstrip().encode('utf+8')).hexdigest()
        if hashValue not in completed_lines_hash:
            output_file.write(line)
            completed_lines_hash.add(hashValue)
    
     output_file.close()  
     tkMessageBox.showinfo("success", "Accounts created and all duplicate items are ignored")
    
    
    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 and  deleteuserpwd.get() == "Pa$$w0rd123" :
           tkMessageBox.showinfo("success", "Account Deleted")
           end()
    
    
    
    def du():
    du.filename = tkFileDialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("text document","*.txt"),("all files","*.*")))
    print(du.filename)
    with open(du.filename, "r") as input_file:
    with open('C:/FreeRADIUS.net/etc/raddb/users.conf', 'w+') as output_file:
    for line in input_file:
        if "User" in line: continue
        output_file.write()
    
    output_file.close() 
    tkMessageBox.showinfo("success", "Accounts deleted")
    

Tags: 用户in密码netconfaswithline

热门问题