大家好我正在研究我的最终项目,我正在创建一个目录窥探器
所以这是这个脚本的底线:
我的问题:
保存CSV时。它不会产生CSV,我收到此错误 " ValueError:关闭文件" .asksaveasfilename()的I / O操作是 不工作
GUI文本滚动框无效并打印出信息 我需要
下面是我的剧本。任何人都可以帮我这个吗?
from Tkinter import Tk
from tkFileDialog import askdirectory
from array import *
import os
version = '1.0'
import os
import csv
from Tkinter import BOTH, LEFT, TOP, END, StringVar
from ttk import Frame, Entry, Button, Label
from ScrolledText import ScrolledText
from tkFileDialog import askdirectory
from tkFileDialog import asksaveasfilename
class FileWalkerWindow(Frame):
def __init__(self):
Frame.__init__(self)
self.pack(expand=True, fill=BOTH)
self.master.title("Directory Snooper v" + version)
self.master.iconname("Directory Snooper")
self.dir = StringVar() # tkinter does not work with standard python variables
self.dir.set(os.getcwd()) # set to current working directory
description = "This program walks through a directories " \
+ "print out name of directory file path. " \
+ "prints out mumber of files in your in your directory. " \
+ "It list files and tests for corrupted zipfiles and " \
+ "creates a CSV file of the findings"
row1 = Frame(self)
Label(row1, text="Choose Directory:").pack(side=LEFT, pady=10)
self.dir_ent = Entry(row1, width=80, textvariable=self.dir)
self.dir_ent.pack(side=LEFT)
Button(row1, text="Browse", width=10, command=self.browse).pack(side=LEFT, padx=5)
row1.pack(side=TOP, ipadx=15)
row2 = Frame(self)
btn = Button(row2, text="Snoop", command=self.savefile, width=15)
btn.pack(side=LEFT, padx=5, pady=10)
row2.pack(side=TOP)
self.output = ScrolledText(self, height=15, state="normal",
padx=10, pady=10,
wrap='word')
self.output.insert(END, description)
self.output.pack(side=LEFT, fill=BOTH, expand=True, padx=5, pady=5)
self.bind('<Key-Return>', self.savefile) # bind enter press to walk
def browse(self):
dirpath = askdirectory(parent=self, title="Select Directory")
self.dir.set(dirpath)
def savefile (self):
self.output.delete(1.0, END)
name=asksaveasfilename()
with open(name,'w') as csvfile:
dirList = os.listdir(self.dir.get())
data = ((fname, str(os.path.getsize(self.dir.get() + "/" + fname)), str(os.path.splitext(fname)[1]),) for
fname in
dirList)
for entry in data:
create=csv.writer(csvfile)
create.writerow(','.join(entry) + '\n')
csvfile.close()
if __name__ == '__main__':
FileWalkerWindow().mainloop()
答案 0 :(得分:0)
保存CSV时。它不会产生CSV,我收到此错误 “ValueError:关闭文件的I / O操作”
你执行关闭for循环中的文件:
for entry in data:
create=csv.writer(csvfile)
create.writerow(','.join(entry) + '\n')
csvfile.close()
取消它以在for循环后关闭文件。您应该将csv.writer()
移到for
循环之外(如评论中所述):
create=csv.writer(csvfile)
for entry in data:
create.writerow(','.join(entry) + '\n')
csvfile.close()
GUI文本滚动框无效并打印出我需要的信息
问题是您何时想要显示信息!
您可以添加一个按钮来打印所选目录的内容,或者在用户选择要浏览的文件夹时自动打印。
要执行后者,您可以编辑browse
方法,该方法应如下所示:
def browse(self):
dirpath = askdirectory(parent=self, title="Select Directory")
self.dir.set(dirpath)
self.output.insert(END, "\n" + "\n".join(os.listdir(dirpath)))
如何向我的CSV添加标题,以便我的CSV具有 文件名,扩展名,大小和注释
在编写实际值之前,只需编写一个包含标题的Python字符串:
with open(name,'w') as csvfile:
csvfile.write("filename,extension,size,comment")
dirList = os.listdir(self.dir.get())
# some other stuff after...
另请注意,虽然Python指南强烈建议您不要使用from module import *
,但导入Tkinter
的标准方法是from Tkinter import *
:)