2010-02-16 96 views
1
from Tkinter import * 
import socket, sys, os 
import tkMessageBox 

root = Tk() 
root.title("File Deleter v1.0") 
root.config(bg='black') 
root.resizable(0, 0) 

text = Text() 
text3 = Text() 

frame = Frame(root) 
frame.config(bg="black") 
frame.pack(pady=10, padx=5) 

frame1 = Frame(root) 
frame1.config(bg="black") 
frame1.pack(pady=10, padx=5) 


text.config(width=35, height=1, bg="black", fg="white") 
text.pack(padx=5) 

def button1(): 
    try: 
     x = text.get("1.0", END) 
     os.remove(x) 
    except WindowsError: 
     text3.insert(END, "File Not Found... Try Again\n")  

def clear(): 
    text.delete("1.0", END) 

c = Button(frame1, text="Clear", width=10, height=2, command=clear) 
c.config(fg="white", bg="black") 
c.pack(side=LEFT, padx=5) 

scrollbar = Scrollbar(root) 
scrollbar.pack(side=RIGHT, fill=Y) 
text3.config(width=35, height=15, bg="black", fg="white") 
text3.pack(side=LEFT, fill=Y) 
scrollbar.config(command=text3.yview) 
text3.config(yscrollcommand=scrollbar.set) 

w = Label(frame, text="Delete A File") 
w.config(bg='black', fg='white') 
w.pack(side=TOP, padx=5) 


b = Button(frame1, text="Enter", width=10, height=2, command=button1) 
b.config(fg="white", bg="black") 
b.pack(side=LEFT, padx=5) 

root.mainloop() 

我不明白爲什麼刪除代碼不工作,即使文件存在,我也會得到「文件未找到」。刪除文件

+0

「文件未找到」幾乎不是刪除文件時可能出錯的唯一的東西。異常實際上報告了什麼? (看看'strerror'場。) – badp 2010-02-16 23:46:40

回答

0

我相信gnibbler的軌道上與空白是問題。文本小工具爲您提供了最終的字符\ n。嘗試添加.strip()到您的text.get結束,或者你可以使用輸入窗口小部件,因爲你的文本小部件,而不是一個文本控件只有一行anways。

x = text.get('1.0', END).strip() 
0

或許x是不是你想的是,只是一種猜測,但也許有一個一些空白那裏或財產以後,試試這個檢查

def button1(): 
    try: 
     x = text.get("1.0", END) 
     print repr(x) 
     os.remove(x) 
    except WindowsError, e: 
     print e 
     text3.insert(END, "File Not Found... Try Again\n") 
3

當我在Linux上運行此代碼,並放置一個斷點button1(),我看到的x的值包括一個尾隨換行符。這意味着os.remove()通話將無法工作,因爲我輸入文件名並沒有實際包含一個換行符。如果我刪除尾隨的換行符,代碼將起作用。

+0

正確的:文本組件是保證始終有一個結尾的新行。原始代碼應該是「x = text.get(」1.0「,」end-1c「)」 – 2010-02-17 12:04:18