Python programming: File reading and writing

0
224
Python programming reading and writing
Python programming reading and writing

Python programming reading and writing: When we write a program, we usually run the data on the screen. Lost one time We want our program to save the data from the user input so that it can be used in the future. At times, it may be necessary to perform an operation to read data from a file. And there are file reading and writing options for Python.

To read data from a file or to put some data in it, you have to open it first. This is done with open (). And open () returns an object. The file name inside the open is to be given. Assuming that the directory where we put the Python program, there is a file named file.txt in the same directory/folder. If not, create it in a txt file. Put some text inside the file.txt.

f = open('file.txt')
content = f.read()
print(content )

Here, in the first line, we open the file as an object. Then we read its contents. And put it in a variable called content. Finally, we have printed the content variable.

Whatever is inside the file, it will output us.

Now if we want to readjust the first line inside the file, for that:

f = open('file.txt')
line = f.readline()
print(line)

We can run a loop to read all the lines one by one, for example

f = open( 'file.txt')
for line in f:
    print(line)

Here we have used them for a loop. The for loop can be found in the Control Statements Chapter.

Keeping some data in file/file writing:

When we open a file using open (), it is open in Reading mode by default. For example, f = open (‘file.txt’), here our file.txt file is open in reading mode. We can only read data from a file. But I can’t keep anything in the file. If we want to write something in a file, we need to change its mood. For example, to open in writing mode:

f = open( 'file.txt','w')

Now the file is open in writing mode. This time we will write something inside:

f = open( 'file.txt','w')
f.write('This is a testn')

When the file is finished, we will close the file. using close (). From now on, we will open the file and close the file by calling close ().

Now if we open the file.txt file, we can see it inside:

This is a test

The previous data has been removed.

If we want to put a number in the file, we have to convert the number to the string first, then put it in the file. As we want to put the value of pi in the file,

pi = 3.14159265359
s = str(pi)
f = open( 'file.txt', 'w' )
f.write(s)
f.close()

When you open the file you can see that the value of pi is stored. We can also add a string to the value of pi if we want:

pi = 3.14159265359
s = str(pi)
f = open( 'file.txt', 'w' )
f.write( 'Value of pi: ' + s)
f.close()

When it’s stored on the file: Value of pi: 3.14159265359

These are very common tasks. Although using this simple operation we can do many things.

Note: More Info (#Tech24InfoBD)( Python programming reading and writing )

LEAVE A REPLY

Please enter your comment!
Please enter your name here