我在工作中一直使用Filezilla来ftp文件。我希望让其他人访问ftp文件,但我们不希望他们拥有filezilla提供的完全访问权限。我试图创建一个基本的python脚本,将执行以下操作: 1.让用户从文件对话框中选择一个文件 2.获取该文件并将其上传到FTP服务器 3.向用户返回一条消息,说明您的文件已成功上传。
如果我硬编码python根目录中的文件名,我能够成功连接到FTP服务器。我正在努力将带有参数的函数传递给tkinter按钮。我希望文件由一个函数接收,然后由另一个函数处理。任何帮助都将不胜感激,我是一个主要的蟒蛇菜鸟,我确信我错过了一些明显的东西...
import sys
from ftplib import FTP
from Tkinter import *
import tkFileDialog
#import Tkinter as ttk
def launch_file_dialog_box():
raw_filename = tkFileDialog.askopenfilename()
return raw_filename
def upload_file_to_FTP(raw_filename):
## first thing we do is connect to the ftp host
ftp = FTP('')
ftp.login( user = '', passwd='')
ftp.cwd("")
ftp.set_pasv(False)
file_name = raw_filename
file = open(file_name, 'rb')
ftp.storbinary('STOR ' + file_name, file)
file.quit()
App = Tk()
App.geometry("600x400+200+200")
App.title("Upload a Program Flyer to the Library Website")
Appbutton = Button(text='Choose a File to Upload', command = launch_file_dialog_box).pack()
Appbutton_FTP = Button(text='Upload File to FTP Server', command = upload_file_to_FTP(raw_filename)).pack()
App.mainloop()
答案 0 :(得分:0)
使用self
关键字在多个函数定义中使用相同的变量。创建类,然后创建对象,稍后调用mainloop()
import sys
from ftplib import FTP
import Tkinter as tk
import tkFileDialog
#import Tkinter as ttk
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.geometry("600x400+200+200")
self.title("Upload a Program Flyer to the Library Website")
self.Appbutton = tk.Button(text='Choose a File to Upload', command = self.launch_file_dialog_box).pack()
self.Appbutton_FTP = tk.Button(text='Upload File to FTP Server', command = self.upload_file_to_FTP).pack()
def launch_file_dialog_box(self):
self.raw_filename = tkFileDialog.askopenfilename()
def upload_file_to_FTP(self):
## first thing we do is connect to the ftp host
ftp = FTP('')
ftp.login( user = '', passwd='')
ftp.cwd("")
ftp.set_pasv(False)
file_name = self.raw_filename
file = open(file_name, 'rb')
ftp.storbinary('STOR ' + file_name, file)
file.quit()
app = App()
app.mainloop()