python - Reading a text document and converting it to a string -


i'm trying make basic script reads specified text document, asks string of characters replace , replace it, saves it:

from tkinter import tk, filedialog tkinter.filedialog import askopenfilename  tk().withdraw()     def openfile():     filename = askopenfilename(initialdir="c:/users", filetypes = (("text file", "*.txt"),("all files","*.*")), title = "open document.")     open(filename, 'r') f:         textfile = f.read()  openfile() print(textfile) 

there's error when running script , selecting file

traceback (most recent call last):   file "c:\users\****\documents\python\find , replace\replace.py", line 11, in <module> print(textfile) nameerror: name 'textfile' not defined 

game on: find what's different:

from tkinter import tk, filedialog tkinter.filedialog import askopenfilename  tk().withdraw()  def openfile():     filename = askopenfilename(initialdir="c:/users", filetypes = (("text file", "*.txt"),("all files","*.*")), title = "open document.")     open(filename, 'r') f:         textfile = f.read()         return textfile # return file content here visible main function  textfile = openfile() # output here can print on next line print(textfile) 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -