Monday, 1 August 2016

Tutorial: File Reading and Writing Methods

Create an empty text file.

>>>fo = open("my_file.txt", "w")        # The file is newly created  
>>>fo.close()                                    # Closing file. VERY IMPORTANT!

Now we create an empty file "my_file.txt" with mode "w".

Write to the file.

Next i am going to write something to this file, for that we have to open the file with "w" write mode ok

>>>fw = open("my_file.txt", "w")     
>>>fw.write("Hai Friends..")                # .write(str)

Read from a file.

Next i am going to read the content from this file and print it on the console, for that open the file with "r" mode

>>>fr = open("my_file.txt", "r")
>>>content =                       # .read()
>>>print content

Tutorial: How to Run a Python Program on Terminal

Craete a python file.

First i am going to create a simple python program to print "HAI FRIENDSSS", For that open a text file and write "print "HAI FRIENDSSS"", And save that text file with extension ".py".

Then open the terminal and go to the folder where the python file is saved

Run python command on terminal.


 Then you will get the out put "HAI FRIENDSSS". In this way you can run python programs on terminal