Skip to main content
 首页 » 编程设计

python之在 Ubuntu 上的 Python GUI 中保留/显示传输文件的名称

2025年05月04日23jirigala

我正在 Ubuntu 上用 python 编写一个程序,将文件从远程 Linux 机器传输到我的主机 Ubuntu 机器,两台机器都通过 CAT5 电缆直接相互连接。我的程序成功从其他 Linux 机器获取文件并显示传输的文件数。 但是我想添加一个从远程 linux 机器到主机的传输文件列表,以及传输文件的数量。

能否请您帮助我改进编码以获得所需的结果。

import io,sys,os,subprocess 
import Tkinter,ttk  
from Tkinter import * 
import tkMessageBox 
 
def transFile(): 
    num_files = len([f for f in os.listdir(path)if os.path.isfile(os.path.join(path, f))]) 
    previous =num_files                    # Count Number of Files before Transferred New Files 
    ip ="192.168.2.34" 
    os.system("rsync -rav pi@"+ip+":python ~/") # Command To transfer Files 
    num_files = len([f for f in os.listdir(path)if os.path.isfile(os.path.join(path, f))]) 
    present = num_files-previous  
    tkMessageBox.showinfo (" File Transfer", 'Number of Files Transfer is '+ str(present)+ '!') 
 
def exitFiles(): 
    root.quit() 
 
root = Tk() 
 
mainframe = ttk.Frame(root, padding="200 200 200 200") 
mainframe.grid() 
 
ttk.Button(mainframe, text="File Transfer", command=transFile).grid (column=10, row=3) 
ttk.Button(mainframe, text="Exit",          command=exitFiles).grid (column=95, row=3) 
 
global process 
path = os.path.expanduser("~/python")           # Define path To play, delete, or rename video  
root.mainloop() 

请您参考如下方法:

我添加了最后五行,它会向您显示文件夹中的所有文件,也许您会在更多解析后得到所需的答案

def transFile(): 
    num_files = len([f for f in os.listdir(path)if os.path.isfile(os.path.join(path, f))]) 
    previous =num_files                    # Count Number of Files before Transferred New Files 
ip ="192.168.2.34" 
os.system("rsync -rav pi@"+ip+":python ~/") # Command To transfer Files 
num_files = len([f for f in os.listdir(path)if os.path.isfile(os.path.join(path, f))]) 
present = num_files-previous  
tkMessageBox.showinfo (" File Transfer", 'Number of Files Transfer is '+ str(present)+ '!')     
a=0 
for file in os.listdir('.'): 
    if fnmatch(file, '*.*'): 
        a +=1 
        ttk.Label(mainframe, text=file).grid(row=4+a, column=10)