2016-04-19 28 views
0

こんにちは、私は私の最終的なプロジェクトに取り組んでいると私は、だからここディレクトリ盗聴PythonのディレクトリリーダーのTkinter GUIが正しく

を作成しています誰もが、このスクリプトのbottomlineでは動作しない:Tkinterのと

  1. GUIをすることをユーザにディレクトリを尋ねて、ユーザに のcvsファイルを自分が選択した場所に保存するように促します。 CSVには というファイル名、ファイル拡張子、ファイルサイズが含まれており、ユーザが入力したディレクトリ から取得します。
  2. 私のGUIは、それが行われたとき スクリプトと印刷のプロセスを印刷する必要がありますテキストスクロールボックス、

私の問題を抱えている:

  • CSVを保存します。これは、CSVを生成していないと私は、このエラー
    取得: です)(.asksaveasfilenameを「とValueError閉じられたファイルにI/O操作を」作業や情報 をプリントアウトされていない

  • GUIのテキストスクロールボックスを動作していません私は私のCSVが
    、ファイル名、拡張子、サイズを有し、かつ

をコメントするように、私は私のCSVにヘッダを追加するにはどうすればよい

  • が必要

    以下は自分のスクリプトです。誰もこれで私を助けることができますか?

    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

When saving CSV. It does not produce a CSV and I get this error "ValueError: I/O operation on closed file"

あなたはは、あなたのforループ内のファイルクローズん:それはのためのループの後、ファイルを閉じます

for entry in data: 
    create=csv.writer(csvfile) 
    create.writerow(','.join(entry) + '\n') 
    csvfile.close() 

インデント解除を。 (コメントで述べたように)あなたにもforループのcsv.writer()外を移動する必要があります。

create=csv.writer(csvfile) 
for entry in data: 
    create.writerow(','.join(entry) + '\n') 
csvfile.close() 

GUI text scroll box is not working and printing out the information i need

情報を表示したいときの質問です!
選択したディレクトリの内容を印刷するボタンを追加したり、ユーザーが参照するフォルダを選択したときに自動的に印刷することができます。

後者を行うには、あなたはあなたのようになりますbrowse方法、編集することができます:ちょうど実際の値を書き込む前に、あなたのヘッダーを含むPythonの文字列を書く

def browse(self): 
    dirpath = askdirectory(parent=self, title="Select Directory") 
    self.dir.set(dirpath) 
    self.output.insert(END, "\n" + "\n".join(os.listdir(dirpath))) 

How do I add headers to my CSV so that my CSV will have filename,extension,size, and comment

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 *です。

+0

は、「close」に関する正しい提案ですか?すべての反復で新しい 'csv.writer'を作成しています。ループの外に移動する必要があるようです。つまり、毎回開いたり閉じたりするか、ループの前に開いてから閉じます。 –

+0

あなたが正しいです、すべての反復で 'cvs.writer()'の作成は悪いことですが、解決するために私の答えを更新します。しかし、私はループ内にある 'csvfile.close()'から来たオリジナルの問題に対処しました:) – filaton

関連する問題