2012-05-20 48 views
-2

我需要一些幫助。我用這個方法來幫助我打開一個文件對話框:從Python中的方法引用文件

from Tkinter import * 
import tkFileDialog 

fileOpen = Tk() 
fileOpen.withdraw() #hiding tkinter window 

file_path = tkFileDialog.askopenfilename(
title="Open file", filetypes=[("txt file",".txt"),("All files",".*")]) 

if file_path != "": 
    print "you chose file with path:", file_path 

else: 
    print "you didn't open anything!" 

print file_path 

它工作正常,並且該文件在Python打開,但我不知道如何使該文件進一步的命令,例如,如果我想讀它或寫在它上面。
我曾嘗試引用它作爲fileOpen,但似乎並沒有工作,我不知道該變量會是什麼。

回答

5

該文件根本沒有打開。要打開它,請致電open(file_path)。這給你一個對象,你的罐頭readwrite到。請閱讀Python教程的section on file I/O

+0

我對這些方法很熟悉,但事情是,我需要能夠選擇我想要的任何文件,而不僅僅是一個特定的文件。我會怎麼做? – jesusu2

+0

@ jesusu2:你用'open(file_path)'打開文件。 –

相關問題