Ich habe ein wenig Mühe, einen Dateipfad zu erhalten, so dass ich meine Daten aus der angegebenen (Text-) Datei öffnen und ausführen kann. Unten ist der Code, den ich bisher geschrieben haben:Dateipfad in Python 2.7
================ RESTART: C:\Users\profcs\Desktop\BD TEST.py ================
<open file u'C:/Users/profcs/Desktop/sample.txt', mode 'r' at 0x01EFF128>
Wie ich diesen Weg filtern und 'C:/Users/profcs/Desktop/sample.txt'
bekommen nur so:
def pickfile():
options={}
options['defaultextension'] = '.txt'
options['filetypes'] = [('all files','.*'), ('text files', '.*txt')]
options['initialfile'] = 'sample.txt'
options['initialdir'] = 'C:\Users\profcs\Desktop'
filename=open(tkFileDialog.askopenfilename(**options))
if filename:
print(filename)
return
with open(filename, 'rb') as f:
reader = csv.reader(f)
try:
for row in reader:
print row
except csv.Error as e:
sys.exit('file %s, line %d: %s' % (filename, reader.line_num,e))
but1 = Button(widget1, text='Pick Your File', command=pickfile)
but1.pack(side=BOTTOM, padx=10, pady=1, anchor=SE)
but1.config(relief=RAISED, bd=2)
Wenn ich einen Dateinamen angezeigt werden, habe ich jetzt den Weg in dieser Form erhalten dass ich meine Datei öffnen kann?
Vielen Dank im Voraus.