Open a File in Python Step. Determine the name of the file you wish to open. If the file does not already exist, you can create the file automatically with the file name you choose. Before we can write our program, we have to create a Python programming file, so create the file files.py with your text editor. To make things easy, save it in the same directory as our days.txt file: /users/sammy/.
To make a text editor we need a window. Type in the file: import sys v=sys.python_version if '2.7' in v: from Tkinter import * elif '3.3' in v or '3.4' in v: from tkinter import * root=Tk('Text Editor') root.mainloop() Then if you are on a mac go Cmd+Shift - search and open 'terminal' - then type python /pathto/texteditor. App for mac to download youtube videos. py and hit enter. If you are on a windows, search and open Command prompt, type python /pathto/texteditor.py and hit enter.
Creating A File In Python
![Python Python](https://cdn2.macworld.co.uk/cmsdata/features/3635912/how_to_use_python_on_mac_calculation_1600.jpg)
You should se a screen that looks like the picture above. You need to be able to save your text of course, so we will add a button to save. Under import Tkinter add import tkFileDialog and under import tkinter add import tkinter.tkFileDialog Under the last line you added write this: def saveas(): global text t = text.get('1.0', 'end-1c') savelocation=tkFileDialog.asksaveasfilename() file1=open(savelocation, 'w+') file1.write(t) file1.close() button=Button(root, text='Save', command=saveas) button.grid() Clicking on the button will save your file. The full code: import sys v=sys.version() if '2.7' in v: from Tkinter import * import tkFileDialog elif '3.3' in v or '3.4' in v: from tkinter import * import tkinter.tkFileDialog root=Tk('Text Editor') text=Text(root) text.grid() def saveas(): global text t = text.get('1.0', 'end-1c') savelocation=tkFileDialog.asksaveasfilename() file1=open(savelocation, 'w+') file1.write(t) file1.close() button=Button(root, text='Save', command=saveas) button.grid() root.mainloop(). Under the last line you added, add this: def FontHelvetica(): global text text.config(font='Helvetica') def FontCourier(): global text text.config(font='Courier') font=Menubutton(root, text='Font') font.grid() font.menu=Menu(font, tearoff=0) font['menu']=font.menu helvetica=IntVar() courier=IntVar() font.menu.add_checkbutton(label='Courier', variable=courier, command=FontCourier) font.menu.add_checkbutton(label='Helvetica', variable=helvetica, command=FontHelvetica) Congratulations! You have finished a very simple text editor. If you want more tutorials like this tell me what.
Run the file to use it! This whole thing is broken for python 3.
I will say, this is a great learning tool for an intro into this.
![Edit Edit](http://www.atmos.umd.edu/~bjohnson/pyearth/graphics/hello.png)
When I began learning Python, one of the first things I needed to know how to do was open a file. Now, the term “open a file” can mean different things depending on the context. Sometimes it means to actually open the file with Python and read from it, like with a text file. Other times, it means “to open the file in its default program”; and sometimes it means, “open the file in the program I specify”. So, when you go looking for how to do the latter two, you need to know how to ask Google just the right question or all you’ll end up with is learning how to open and read a text file. In this article, we’re going to cover all three and we’ll also show how to open (or run) programs that are already installed on your PC.
Because that topic is also one of the first things I needed to learn and it uses some of the same techniques. How to Open a Text File Let’s start by learning how to open a file with Python. In this case, what we mean is to actually use Python to open it and not some other program. For that, we have two choices (in Python 2.x): open or file. Let’s take a look and see how it’s done!
# the open keyword opens a file in read-only mode by default f = open ( 'path/to/file.txt' ) # read all the lines in the file and return them in a list lines = f. Readlines ( ) f. Close ( ) As you can see, it’s really quite easy to open and read a text file. You can replace the “open” keyword with the “file” keyword and it will work the same. If you want to be extra explicit, you can write the open command like this instead: f = open ( 'path/to/file.txt', mode= 'r' ) The “r” means to just read the file.
You can also open a file in “rb” (read binary), “w” (write), “a” (append), or “wb” (write binary). Note that if you use either “w” or “wb”, Python will overwrite the file, if it exists already or create it if the file doesn’t exist. If you want to read the file, you can use the following methods: • read – reads the whole file and returns the whole thing in a string • readline – reads the first line of the file and returns it as a string • readlines – reads the entire file and returns it as a list of strings You can also read a file with a loop, like this: f = open ( 'path/to/file.txt' ) for line in f: print line f. Close ( ) Pretty cool, huh? Python rocks!