Sunday, October 23, 2016

Using text file with python 2.7 .

This simple tutorial is about text file. The text file come with txt extension.
It is a file containing text that can be used and further processing by functions and modules python.
I used Tkinter python module. The reason I chose this python mpodule is:
  • old python module;
  • rapidly developing graphical interfaces.
First step is to import the python module and then to put all of dir command into one text file.
The name of this file is: Tkinter_funct.txt
Open your python and try this source code:
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
>>> from Tkinter import *
>>> Tkinter_all=dir(Tkinter)
>>> Tkinter_file=open('Tkinter_funct.txt','w')
>>> Tkinter_file.write(str(Tkinter_all))
>>> print Tkinter_all

You will see the content of dir python module.
The text file is also have this text output.
To read the file is need to open the file with open function, to put position for read by using seek function and to read with read function.
This example will show you how is working:
>>> test=open('Tkinter_funct.txt','r+')

Will open the file named Tkinter_funct.txt and r+ access to file.
The position it is set to 1.
The read function make all with full content output.
Now let's see the next steps, by change the read and seek values.

"CTIVE', 'A"
"LL', 'ANCHOR', 'ARC'"

This outputs come with parts of all content and show you how it's working.