# -*- coding: gbk -*-
import os
import os.path
import time
import sys
import subprocess
contents = 'I:\\Ñ¸À×ÏÂÔØ'
print(contents)


s =[]
error_files=[]
pass_lists = os.listdir(contents)
pass_lists2 =[]
for i in pass_lists:
    j  = i.replace('ÐÇºÅ','*')#.replace('&','^&')
    pass_lists2.append(j)
print(pass_lists2)
for root, dirs, files in os.walk(contents):
    nested_levels = root.split('\\')
    #if len(nested_levels) ==4:
    #    del dirs[:]
    #del dirs[:]½«É¾³ýÁÐ±íµÄÄÚÈÝ£¬¶ø²»ÊÇÓÃ¶ÔÐÂÁÐ±íµÄÒýÓÃÌæ»»dirs¡£ÕâÑù×öÊ±£¬¾ÍµØÐÞ¸ÄÁÐ±íºÜÖØÒª¡£

    for name in files:

        if ('7z' in name[-4:] ) or('zip' in name[-4:] )  :
        
            print('         --------')
            END_ = 0
            #print(f'name ={name}')
            #print(f'dirs ={dirs}')
            #print(f'root ={root}')
            a_ = root[len(contents):]
            #print(f'a_ ={a_}')
            a2_ = a_.split('\\')
            #print(a2_)
            PASSWORD1_ = a2_[1]
            if "ÐÇºÅ" in PASSWORD1_:
                PASSWORD1_ = PASSWORD1_.replace('ÐÇºÅ','*')
            #print(f'        password ={PASSWORD1_}')
            
            ex_dir = root  +"\\"
            ex_dir = '\"' + ex_dir  + '\"'
            FILENAME1_ = root +'\\'+ name
            FILENAME2_ = '\"' + FILENAME1_  + '\"'
            print(f'        filename1 ={FILENAME1_}')

            #½âÑ¹Á÷³Ì
            SSTR = ' '.join(["7z.exe","x -aoa ",FILENAME2_,f"-p{PASSWORD1_}",f"-o{ex_dir}",])
            print(f"        SSTR = {SSTR}")
            obj = subprocess.Popen(SSTR,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
            cmd_out,cmd_error = obj.communicate()
            #print(cmd_out)
            if "Everything is Ok" in cmd_out:
                #print(f'½âÑ¹³É¹¦')
                print('\033[1;32m' + '½âÑ¹³É¹¦' + '\033[0m')
                print(f'É¾³ýÎÄ¼þ{FILENAME1_}')
                os.remove(FILENAME1_)
            else:
                errot_2 = cmd_error.strip()[:50]
                print("             ",errot_2)
                print(f'                ÎÄ¼þ¼ÐÃÜÂëÊ§Ð§ ')
                if "Wrong password" in cmd_error:
                    for i in pass_lists2:#
                        SSTR = ' '.join(["7z.exe","x -aoa ",FILENAME2_,f"-p{i}",f"-o{ex_dir}",])
                        print(f"                                   ³¢ÊÔÃÜÂë  {i}                ",end='\r')
                        obj = subprocess.Popen(SSTR,bufsize=1 , stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
                        cmd_out,cmd_error = obj.communicate()
                        #print(cmd_error)
                        if "Everything is Ok" in cmd_out:
                            print("")
                            print("")
                            print("")
                            print('\033[1;32m' + '½âÑ¹³É¹¦' + '\033[0m')
                            print(f'½âÑ¹³É¹¦£¡£¡£¡£¡£¡£¡£¡ÃÜÂë={i}£¡£¡£¡£¡£¡£¡£¡£¡')
                            with open(FILENAME1_+f"³É¹¦ÃÜÂë{i}.txt",mode="w",encoding="utf-8") as f:  #Ð´ÎÄ¼þ,µ±ÎÄ¼þ²»´æÔÚÊ±,¾ÍÖ±½Ó´´½¨´ËÎÄ¼þ
                                pass
                            print(f'É¾³ýÎÄ¼þ{FILENAME1_}')
                            os.remove(FILENAME1_)
                            END_ =1
                            print(f'')
                            time.sleep(1)
                            break
                        else:
                            pass
                    if END_ == 1:
                        continue #ÏÂÒ»ÎÄ¼þ
                    if END_ == 0:
                        print("")
                        print("")
                        SSTR = ' '.join(["7z.exe","x -aoa ",FILENAME2_,f"-o{ex_dir}",])
                        #print(f"                                 ³¢ÊÔÎÞÃÜÂë½âÑ¹                ",end='\r')
                        obj = subprocess.Popen(SSTR,bufsize=1 , stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
                        cmd_out,cmd_error = obj.communicate()
                        #print(cmd_error)
                        if "Everything is Ok" in cmd_out:
                            print('\033[1;32m' + '½âÑ¹³É¹¦' + '\033[0m')
                            #print(f'½âÑ¹³É¹¦')
                            print(f'£¡£¡£¡£¡£¡£¡£¡£¡ÎÞÃÜÂë£¡£¡£¡£¡£¡£¡£¡£¡')
                            with open(FILENAME1_+f"ÎÞÃÜÂë.txt",mode="w",encoding="utf-8") as f:  #Ð´ÎÄ¼þ,µ±ÎÄ¼þ²»´æÔÚÊ±,¾ÍÖ±½Ó´´½¨´ËÎÄ¼þ
                                pass
                            print(f'É¾³ýÎÄ¼þ{FILENAME1_}')
                            os.remove(FILENAME1_)
                            continue##ÏÂÒ»ÎÄ¼þ
                        else:
                            print("")
                            print("")
                            print("")
                            print(f"                ½âÑ¹Ê§°Ü ÃÜÂëÎ´Öª")
                            error_files.append(FILENAME1_)
                            continue#ÏÂÒ»ÎÄ¼þ
                    #print("´ËÐÐ")

print('=============================================')
print('end \n\r Î´½âÑ¹ÎÄ¼þÈçÏÂ')
for i in error_files:
    print(i)
    