File in python #1 : Read file and Write file and Modes file

in #utopian-io6 years ago (edited)

Repository

https://github.com/python

What Will I Learn?

  • Read File
  • Write File
  • and Modes File

Requirements

  • Basic Python
  • Install Python 3
  • Install Flask

Resources

Difficulty

Basic

Tutorial Content

This tutorial, we will interact with the file using python. for those of you who don't know what Python is, you can see the list of tutorials in the curriculum below. We will learn how to read, write and manipulate files in python. there are some files that we will learn in this tutorial. I suggest you are familiar with the Python programming language. let's just start the following tutorial.

Read File

What we will do first is read the file. Of course before reading the file we must first open the file. We will start reading files that have the extension .txt. For that we can see an example in the following code:

file.py

## Open file
file = open('example.txt', 'r')

result = file.read() // Read file

print(result) // Print result
  • What we will do the first time in the read file is to open the file, to open the file we can use the built-in function of the python, the function is open(). in this function, there are two main parameters. The first is the name of the file to be opened, The second is the mode. The file that we open is example.txt and the mode is 'r', 'r' mode is the default mode.

  • Well to read the file we need to use other functions, namely read(). I will move the data to a variable result = file.read() and we will see the results when we print print(result).

The content of file example.txt

Screen Shot 2018-12-19 at 1.25.30 pm.png

and the Result will be like this:

ezgif.com-video-to-gif (1).gif

Write File

at the top we have learned how to read files, now in this section, we will learn how to write files in python. What must be changed is the mode and function, for more details we can see in the code below:

## Open file

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

file.write('The system is writing in the file example.txt')
result = file.read()

print(result)
  • Because we want to write files we have to change the mode like this open('example.txt', 'w') and to write in the file we can use write() function. You can write the data you write as a function parameter. But we cannot directly write and read the file. we will get an error like the following :

ezgif.com-video-to-gif (2).gif

Screenshot_5.png

to solve the problem above, we need a new mode as a way to run write and read functions simultaneously.

  • Write by replacing the previous data

We can use write and read functions simultaneously by changing the mode like the following:

## Open file

file = open('example.txt', 'r+') //Read and write at the same time

file.write('The system is writing in the file example.txt')
result = file.read()

print(result)

to read and write files simultaneously we can use the 'r+' mode. but If we pay attention when we use the write function, we indirectly replace the previous data with the new data. We can see as shown below:

ezgif.com-video-to-gif (3).gif

This can happen because if we use the 'r +'mode it means we will repeat writing from the beginning of the file.

  • Write by combining the previous data

to combine existing data with the data we are writing we can change the mode to 'a+'. In this mode it means we will write at the end of the file, so we do not replace the data that already exists. For more details, we can see in the code below:

## Open file

file = open('example.txt', 'a+') //Read and write at the same time

file.write('This is an additional word')
result = file.read()

print(result)
  • to combine previous content with the next content, we can use mode 'a+'
    We can see in the image below the following results from file.py.

ezgif.com-video-to-gif (4).gif


  • Read, Write and Print simultaneously

At the top, we get an error message. we cannot read and write simultaneously but we can do a little trick, to run these functions simultaneously, then when we print we need to return the pointer at the beginning of the file. for more details, we can see the code below:

## Open file

file = open('example.txt', 'r+')

file.write('The system is writing in the file example.txt')

file.seek(0)
result = file.read()

print(result)
  • We can use the seek() function to move the pointer at the beginning of the file. The initial value starts at 0, so we can pass 0 as the parameter seek(0). for more details, we can see the demonstration from the following picture

ezgif.com-video-to-gif (5).gif

We have learned the crew stage from our introduction of files in python. Next, we will discuss more deeply about how to manipulate the data that is in a file so that it can be structured data and we can use it for our logic and programming needs. thank you for following this tutorial. sorry if there is an error in writing the word. I hope this tutorial is useful for readers.

Curriculum

  • Web develoment with FLASK

Web developement with python #1 : Flask initialization and Routing system

Web development with python #2 : Templating jinja2 and Method POST on routing system

Web development with python #3 : Get method, Query parameter and Navigate Routing

Web development with python #4: Store cookie and Get cookie in template

Web development with python #5: Web development with python #5 : Session in flask and Login and Logout system

Web development with python #6 : Use flash message and combine it with framework boostrap

Proof of work done

https://github.com/milleaduski/python-web-app

Sort:  

Thank you for your contribution @duski.harahap.
After analyzing your tutorial we suggest the following points below:

  • The subject of python to read and write to a file, is very basic.

  • There are already several online tutorials in which they talk about this subject.

  • It would be interesting to delve deeper into a python subject that is not so usual to see in online tutorials.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you very much @portugalcoin, indeed this first tutorial is very basic. because I want to start from the bottom. in the next tutorial I will discuss it more deeply

Thank you for your review, @portugalcoin! Keep up the good work!

Hi @duski.harahap!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @duski.harahap!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.18
TRX 0.15
JST 0.028
BTC 63283.09
ETH 2463.49
USDT 1.00
SBD 2.54