Monday 1 August 2016

Tutorial: File Reading and Writing Methods

1)
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".


2)  
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)
>>>fw.close()




3)
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 = fr.read()                       # .read()
>>>print content
>>>fr.close()


Tutorial: How to Run a Python Program on Terminal



1)
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

2)
Run python command on terminal.

Syntax:
"python filename.py"




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