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

时间:2019-05-20 05:47:57

标签: python

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

用户名在行的第一个单词中包含5位数字。我不希望在创建用户帐户后在输出文件中重复该用户名。

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

  • 我不希望发生此输出:

  • 81045用户密码==“ 81045”
  • 81045用户密码==“ 56811”
  • 65654用户密码==“ 56287”
  • 65654用户密码==“ 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")
    

0 个答案:

没有答案