Currently received a small task to do a Windows system under the file retrieval, Replication tool. The task uses Python's own GUI tool Tkinter writing interface, after testing, functional stability.
The specific use of the following methods:
(1) above, Box 3 is a tkinter write file retrieval, Replication tool, where "file source path" is entered in box 1 in the path information, "File target path" entered the box 2 in the path information, "To search for file name"
The name of the file with the expanded name is entered in.
Remember to bring the extension. Remember to bring the extension. Remember to bring the extension. (The important thing to say three times.) )
(2) Click "Search" to display the file with the same name under different path information in the target folder in the "Search for file list of the same name";
(3) Click on the target file in the list, click "Copy" to complete the copy operation, and a pop-up box to prompt.
PS: If the target file already exists in the target path, you will be prompted if you want to overwrite it.
PPS: The tool can only achieve a single target file retrieval, replication, follow-up younger brother will be further improved.
The specific implementation code is as follows:
#-*-coding:utf-8-*-import OS import shutil import collections from tkinter Import * Import Tkinter.messagebox def f Ile_find_byname (): Src=src_path.get () Dest=des_path.get () Name=file_name.get () # src= ' C:/users/xsw-hp/desk Top/original ' # dest= ' d:/filecopy ' # name= ' file.txt ' if os.path.exists (src) ==false:str= "The file you entered source path Path does not exist. "Tkinter.messagebox.showwarning (" File_copy Demo, str) print ("The file source path you entered does not exist.) ") Else: #1. A dictionary fp=collections that stores the source folder path and filename information. Ordereddict () #2. The dictionary that stores the destination file ordinal and its path information in the source folder fpp={} count=0 #2.1 The destination file ordinal that is customized according to the retrieval order S=NAME.SPL It ('. ') #2.2 separates the target file name from the extension #3. Search for the destination file in the original folder path for Dn,dns,fns in Os.walk (SRC): for FL in Fns:if (
Os.path.splitext (FL) [0]==s[0]) and (Os.path.splitext (FL) [1][1:]==s[1]): If DN not in Fp.keys (): Fp[dn]=[] Fpp[count]=dn count+=1 Fp[dn].app End (FL) #4. Print a list of target files with the same name under different subdirectories of the original folder the number of destination files is: {0:^3}, as shown in the following figure: '. Format (len (FPP)) file_listbox.delete (0
, end) If Len (FPP) >0:str= ' target file number is: {0:^3}, as shown in the following figure: '. Format (len (FPP)) File_listbox.insert (END,STR) i=0 for K in Fp.keys (): for F F in Fp[k]: item= (' {1:<2}---{0} '). Format (Os.path.join (K,FF), i) file_listbox.ins ERT (End,item) i+=1 else:str= "The destination file you are looking for does not exist. "Tkinter.messagebox.showwarning (file_copy Demo, str) print (" The destination file you are looking for does not exist.)
") def file_copy_byname (): Src=src_path.get () Dest=des_path.get () Name=file_name.get () #5. In the list of target files with the same name, select a target text to be manipulatedPieces if File_listbox.size () >1:i=1 flag=0 while I<file_listbox.size (): If File_
Listbox.selection_includes (i): Flag=1 break i+=1 if flag==1:
Print (i) str1=file_listbox.get (i). Split ('---') [1] relative_path=str1[len (SRC) +1:-len (name)-1] # 6.
Confirm that the target file is in the destination folder in the new path information if Len (Relative_path) ==0:newpath=dest;
New Folder path for Else:newpath=os.path.join (Dest,relative_path) #6.1 files Print (' Relative path is:%s '% relative_path) print (' The new path is:%s '% NewPath) # 7. Copy operation of target file from source folder to target folder if Os.path.exists (NewPath): #7.1 if the new path folder already exists Flag1
=0 flag2=0 for Dns,dnss,fnss in Os.walk (NewPath): #7.1.1 Retrieving target files in the new path folder If Len (DNs) >len (NewPath): Break for FL in fnss:if (fl==name) : Flag3=int (Tkinter.messagebox.askokcancel ("File_copy Demo"), (' target file ' {0} ' already exists!) are you sure you want to overwrite it?
'). Format (Os.path.join (NEWPATH,FL))) flag2=1 if flag3==0: Flag1=1 str= ' You have canceled file copying. ' Tkinter.messagebox.showinfo ("File_copy Demo", str) print ( ' You have canceled file copying.
') break; # print (' target file ' {0} ' already exists! Are you sure you want to overwrite it? '). Format (Os.path.join (NEWPATH,FL)) # confirm=input (' Please enter ' y/n ': '); #7.1.1.1 Whether to overwrite the target file already exists in the new path folder # if confirm== ' Y ': # flag1=0 # Flag2=1 # Break # Else: # flag1=1 # flag2=1 # Print (' You have canceled file copying.
') # break;
if (flag2==1): Break if (flag1==0): #7.1.2 Copy of the target file Oldfile=os.path.join (Src,name) shutil.copy (Str1,newpath) str= ' has been complex Target file "' +oldfile+ '" to "' + newpath+ '". ' Tkinter.messagebox.showinfo ("File_copy Demo", str) ELSE: #7.2 If the new path folder has not been established, directly copy the target file Os.makedirs (NewPath) Oldfile=os.path . Join (Src,name) shutil.copy (Str1,newpath) str= ' has copied the target file ' +oldfile+ ' to ' + newpath+ '.
' Tkinter.messagebox.showinfo ("File_copy Demo", str) Print (str) Else:str= "You have not selected any files. "Tkinter.messagebox.showwarning (" File_copy Demo ", str) print (str) else:str= "The file you are looking for does not exist. "Tkinter.messagebox.showwarning (" File_copy Demo ", str) print (str) # 0. Tkinter the structure layout of the Client interface Root=tk () root.title ("File-copy-by-name") Label (root,width=15,text= ' File source path: ', Relief=ridge). Grid ( ROW=0,COLUMN=0,STICKY=W) Src_path=entry (Root,width=30,relief=ridge) Src_path.grid (row=0,column=1,padx=5,pady=3, STICKY=W) Label (root,width=15,text= ' File destination path: ', Relief=ridge). Grid (Row=1,column=0,sticky=w) des_path=entry (Root, Width=30,relief=ridge) Des_path.grid (row=1,column=1,padx=5,pady=3,sticky=w) Label (root,width=15,text= ' to search for filename: ', Relief=ridge). Grid (ROW=2,COLUMN=0,STICKY=W) file_name=entry (Root,width=23,relief=ridge) File_name.grid (row=2, Column=1,padx=5,sticky=w) File_search=button (Root,padx=3,text= ' search ', command=file_find_byname) File_search.grid (row=2,column=1,padx=5,pady=3,sticky=e) Label (Root, width=39,pady=2,text= ' Search for file list of the same name ', Relief=ridge. Grid (ROW=3,COLUMN=0,COLUMNSPAN=2,PADX=3,PADY=3,STICKY=W) file_ Copy=button (root,padx=3,text= ' copy ', Command=file_copy_byname) File_copy.grid (row=3,column=1,padx=5,pady=3,sticky= E) File_listbox=listbox (Root,width=45,height=5,selectmode=browse,relief=ridge) File_listbox.grid (row=4,column=0, columnspan=2,padx=3,pady=5) Root.mainloop () # file_copy_byname (' c:/users/xsw-hp/desktop/original ', ' D:/FileCopy ', ' finddup.py ') #0.
' File.txt ' filepath.txt ' systest.py ' finddup.py ' new text document. txt '