Sunday, September 16, 2018

Python Ninja Bootcamp 10-File

Learn Python like a Professional! Take You from 0 to Hero




In this Python lecture, we will learn about the File.

"File is an object that stores data or information".

File operation takes place in the following order:
  1. Open the file
  2. Write or Read
  3. Close the file


Let's look at File example-

Create File 


1. Open-File

We have built-in function open() to open the file

f=open('New_file.txt',mode='w') # Open a file for writing


pwd # New file is created in present working directory
>> 'C:\\Users\\mayank singh\\Documents'

2. Write-file

We have write() function to write the string in a file

f.write('Hi Welcome to file lecture') # returns the number of character in the file
>> h

3. Close the file

f.close()  #close and release the resourse allocated to file


Another method of Creating File

The best way to create by using the with statement.

This ensures that the file is closed when the block inside with is exited.
We don't need to explicitly call the close( ) method. It is done internally.

with open('New_file2.txt' ,'w') as f : # Open a file for writing
    f.write('We are in New_file2')

It is designed to provide much cleaner syntax and exceptions handling when you are working with code.


Read-file

We have the read( ) function to read the file that already exists.

f=open('New_file.txt',mode='r') # open the file in read mode


f.read() #  reads and returns the entire file content
>> ' Hi Welcome to file lecture '


f.read() # returns empty file
>> ' '

This happened because first time when we call the read function the cursor reached at the end of the file.
That's why nothing pops up after calling read function for the second time.
We can change the current file cursor position using the seek() method.

f.seek(0) # reset the postion at the beginning of the file
f.read() #  reads and returns the entire file content
>> ' Hi Welcome to file lecture '


f.seek(0) # reset the postion at the beginning of the file
f.readline() #  returns the list of lines from the file
>> [ ' Hi Welcome to file lecture ' ]

Also, try the read() with the file New_file2.txt


File & Loop

# create a file consist of file between 0 to 9 
with open('Even.txt','w') as feven : # Open a file for writing.
    for j in range(0,10):            # loop through zero to 9.
        if j%2==0 :                  # check the even number between 0 t0 9
            feven.write(str(j)+'\n') # returns the content in the file.

with open('Even.txt','r') as feven : # Open a file for writing.
    print(feven.read())              # reads and returns the entire file content
>> 0
     2
     4
     6
     8


Don't worry if you don't understand this section.
Feel free to skip this topic since we will discuss if and loop concept in detail.


EDITS ARE WELCOMED!!

In the next Blog, we will discuss set. 

https://sngurukuls247.blogspot.com/2018/09/python-ninja-bootcamp-11-set.html

......................................................................................................................................

Follow the link below to access Free Python Lectures-
https://www.youtube.com/channel/UCENc9qI7_r8KMf6-_1R1xnw

Instagram-
https://www.instagram.com/python.india/

View the Jupyter Notebook for this lecture

Download the Jupyter Notebook for this lecture 



Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com

No comments:

Post a Comment