Wednesday 5 July 2017

31 reading lines using for loop in file handling Pyrhon

###############
you'll learn about Python file operations. More specifically, opening a file, reading from it, writing into it, closing it and various file methods you should be aware of.
----------------------
When we want to read from or write to a file we need to open it first. When we are done, it needs to be closed, so that resources that are tied with the file are freed.

Hence, in Python, a file operation takes place in the following order.

Open a file
Read or write (perform operation)
Close the file
---------------------
example
ram=open("C:\\Users\\lachu\\Desktop\\venkata.txt","r")
print(ram.read(25))
ram.close()
"""
ram=open("C:\\Users\\lachu\\Desktop\\venka.txt","r")
#ram.seek(5)
print("file name : ",ram.name)
print("file mode : ",ram.mode)
print("file close : ",ram.closed)
ram.close()
print("file close : ",ram.closed)
f = open("C:\\Users\\lachu\\Desktop\\venkat.txt","r")
print(f.read())
f.close()
#C:\\Users\\lachu\\Desktop\\venkat.txt
"""
---------
Opening a file
Python has a built-in function open() to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingly.

 f = open("test.txt")    # open file in current directory
 f = open("C:/Python33/README.txt")  # specifying full path
We can specify the mode while opening a file. In mode, we specify whether we want to read 'r', write 'w' or append 'a' to the file. We also specify if we want to open the file in text mode or binary mode.

The default is reading in text mode. In this mode, we get strings when reading from the file.

On the other hand, binary mode returns bytes and this is the mode to be used when dealing with non-text files like image or exe files.

Python File Modes
Mode    Description
'r'    Open a file for reading. (default)
'w'    Open a file for writing. Creates a new file if it does not exist or truncates the file if it exists.
'x'    Open a file for exclusive creation. If the file already exists, the operation fails.
'a'    Open for appending at the end of the file without truncating it. Creates a new file if it does not exist.
't'    Open in text mode. (default)
'b'    Open in binary mode.
'+'    Open a file for updating (reading and writing)
f = open("test.txt")      # equivalent to 'r' or 'rt'
f = open("test.txt",'w')  # write in text mode
f = open("img.bmp",'r+b') # read and write in binary mode
Unlike other languages, the character 'a' does not imply the number 97 until it is encoded using ASCII (or other equivalent encodings).

Moreover, the default encoding is platform dependent. In windows, it is 'cp1252' but 'utf-8' in Linux.

So, we must not also rely on the default encoding or else our code will behave differently in different platforms.

Hence, when working with files in text mode, it is highly recommended to specify the encoding type.

f = open("test.txt",mode = 'r',encoding = 'utf-8')

No comments:

Post a Comment