File I/O - Text

 Text Files - .txt,.c

Binary files - .jpg,.dat

f=open("file name",opening type)

opening types:

"r" - Read - Default value. Opens a file for reading, error if the file does not exist

"a" - Append - Opens a file for appending, creates the file if it does not exist

"w" - Write - Opens a file for writing, creates the file if it does not exist

"x" - Create - Creates the specified file, returns an error if the file exists , [write only]

r+,a+,w+  == read and write

Difference between r+ and w+ (maybe) - r+ pointer at beginning and w+ pointer at end of file

‘rb’ will open for read in binary mode

‘rt’ will open for read in text mode

Default is reading mode






f.read()=variable

f.close()= very imp to use after our work with a file is done

f.read(n)==read first n characters

f.readline()==read first line

Writing it multiple lines allow it to read lines one by one 

f.readline(n) - Read first n characters till the /n 

If n is negative then it reads the whole line

f.readlines()== read all lines

It return a list with each line as a string and all lines( except the last one) ends with \n

f.write("something")

We need to end \n ourself to print data in new line in write, writelines

f.writelines(iterable object with a line as string at each index) - write multiple lines at once

. Write with 'w' will erase all previous data in file but not erase the data it enter when used multiple times in the same program, it just stores it in the next line

with open("file name","wt") as:

    f.write("something")

    f.close()

Note : Output type of read, read line is str while of readlines is list

with open("file name",opening type) as:

    f.read() #no need to write f.close()

f.tell(empty)==tell on which number of character is the pointer on.

f.seek(offset[, reference point] )==moves the pointer to the specified position by n(offset) characters, reference point : 0 for starting, 1 for current position, 2 for end of file.

 Also offset can be negative. 

Also error if no argument given 

f.flush() - without closing the file object, it writes the contents of file buffer to storage 

.close() flushes any unwritten information and closes the file object 

Properties of file object:

f.name

f.mode

f.encoding

f.closed

f.softspace

f.writable()

f.readable()

Loop through the file line by line:(Latest version only) 

f = open("demofile.txt""r")
for x in f:
  print(x)
All versions:
file = open("demofile.txt""r")
f=file.readlines()
for x in f:
  print(x)
  # here x is a string of a line with \n at end
OR

file = open("demofile.txt""r")
f=file.readline()
for x in f:
  print(x)
  # here x is a character in line

NOTE: read() is like readline() ie charcter collection for all lines combined and readlines() has collection of all lines

To delete a file:

import os
if os.path.exists("demofile.txt"):
  os.remove("demofile.txt")
else:
  print("The file does not exist")

To rename a file:
 os.rename('existing name', 'new name' )

Split vs splitlines (here is d=file.readlines()):


Comments

Popular posts from this blog

Dictionary and Sets in Python

Insertion Sort in python

Grid Printing Code for Python