File Handling

working with files in Python  can be done by using  the open() function.

Syntax for  open() function :

file object = open(file_name [, access_mode][, buffering])

where:  file_name is name of file

   access mode which determines the mode in which file is opened.

Buffer: 1 : line buffer 0 (un buffer ) any positive of that buffer

Negative – system default buffer

Modes

Meaning

Remarks

“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 exist

How to Create a Text File

To create a new file in Python, use the open() method, with one of the following parameters:

  • “x” – Create – will create a file, returns an error if the file exist
  • “a” – Append – will create a file if the specified file does not exist
  • “w” – Write – will create a file if the specified file does not exist
print("x - Create - will create a file, returns an error if the file exist")
my_file = open("myfile.txt", "x")



print("a - Append - will create a file if the specified file does not exist")
my_file = open("myfill.txt", "a")



print("w - Write - will create a file if the specified file does not exist")
my_file = open("myfile.txt", "a")

How to Read Files

To read a file , use read() function

my_file = open("revin.txt", "r")
print(my_file.read())

By default the read() method returns the whole text, but you can also specify how many character you want to return:

print(my_file.read(1))

Read Lines

You can return one line by using the readline() method:

print(my_file.readline())  

Note :  We can also use looping concept to read the whole file

my_file= open("myfile.txt", "r")
for x in my_file:
  print(x)

The close() Method

The close() method of a file object flushes any unwritten information and closes the file object, after which no more writing can be done.

# Open a file

fo = open("foo.txt", "wb")

print("Name of the file: ", fo.name)

fo.close()

The write() Method

  • The write() method writes any string to an open file. It is important to note that Python strings can have binary data and not just text.
  • The write() method does not add a newline character (‘\n’) to the end of the string.
Syntax : fileObject.write(string)
# Open a file

x = open(“revin.txt", "w")

x.write( "Python is a great language.\nYeah its great!!\n")

fo.close()

File Positions

  • The tell() method tells you the current position within the file.
  • The seek(offset[, from]) method changes the current file position.
  • Where Offset argument indicates the number of bytes to be moved
  • Offset – absolute number – no of positions to move forward
  • from argument specifies the reference position from where the bytes are to be moved.
  • Values -0,1,2 (0-beginning ,2-last ,1-current file position)

 

# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10)
print("Read String is : ", str)



# Check current position
position = fo.tell()
print("Current file position : ", position)



# Reposition pointer at the beginning once again
position = fo.seek(0, 0)
str = fo.read(10)
print("Again read String is : ", str)
# Close opend file
fo.close()

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *