Skip to content

Spoke Right

Education for everyone

https://spokeright.com
Primary Menu
  • Home
  • Our Services
    • Translation
    • Interpretation
    • Transcription
    • Voice-Over
    • Proofreading
    • Content writing
  • Free Education
    • Learn Android Studio
    • Learn Python
    • Learn MongoDB
    • Learn MySql
    • Learn React
    • Montessori Education
  • Spoke Right News
  • Earn Online
  • Classic Games by Spoke Right
    • TicTac
    • Checkers
    • Foosball
    • Billiards
    • Master Chess
    • Logic Game
  • Contact Us
Download Now
  • Home
  • Learn Python
  • What is python Files IO
  • Learn Python

What is python Files IO

Spoke Right December 7, 2022 3 min read

Pyhton files io

File handling simply means to open a file and to process it according to the required tasks. Python facilitates several functions to create, read, write, append, delete and close files.

❏     Open File:

To open a file in Python, open() function is used.

Syntax:

f = open(“filename“, “mode“)

Filename:  This parameter specifies the name of the file to be opened.

Mode: This parameter specifies the mode in which the file should be opened.

Different MODES of open function:

$modeMODEDESCRIPTION
rRead only modePointer starts from the beginning of the file.
wWrite only modeOverwrites the existing file or creates a new file if it doesn’t exist. Pointer starts from the beginning of the file.
aWrite only modeContinues writing in the existing file or creates a new file if it doesn’t exist. Pointer starts from the end of the file.
r+Read Write modePointer starts from the beginning of the file.
w+Read Write modeOverwrites the existing file or creates a new file if it doesn’t exist. Pointer starts from the beginning of the file.
a+Read Write modeContinues writing in the existing file or creates a new file if it doesn’t exist. Pointer starts from the end of the file.
rbRead only mode in Binary format.Pointer starts from the beginning of the file.
wbWrite only mode in Binary format.Overwrites the existing file or creates a new file if it doesn’t exist. Pointer starts from the beginning of the file.
abWrite only mode in Binary format.Continues writing in the existing file or creates a new file if it doesn’t exist. Pointer starts from the end of the file.
rb+Read Write mode in Binary format.Pointer starts from the beginning of the file.
wb+Read Write mode in Binary format.Overwrites the existing file or creates a new file if it doesn’t exist. Pointer starts from the beginning of the file.
ab+Read Write mode in Binary format.Continues writing in the existing file or creates a new file if it doesn’t exist. Pointer starts from the end of the file.

     Close File:

To close a file in Python, close() function is used.

Syntax:

f.close()

❏     Read File:

●      To read a file in Python, read() function is used.

Syntax:

f.read()

To read a line of a file in Python, readline() function is used.

Syntax:

f.readline()

❏     Write File:

To write a file in Python, write() function is used.

Syntax:

f.write(statement)

❏     Delete File:

●      To delete a file in Python, os is imported and then os.remove() function is used.

Syntax:

import os

os.remove(“filename“)

To delete a folder in Python, os is imported and then os.rmdir() function is used.

Syntax:

import os

os.rmdir(“filename“)

Python File Handling Methods:

METHODSSYNTAXUSES
rename()os.rename(“existing_file_name”, “new_file_name”)To replace the existing python file name with a new python file name.
remove()os.remove(“file_name”)To delete a python file.
mkdir()os.mkdir(“file_name“)To create a directory to store python files.
chdir()os.chdir(“file_name“)To change the current working directory.
rmdir()os.rmdir(“directory_name”)To delete a directory.
tell()–To get the exact position in the python file.
getcwd()os.getcwd()To get the current working directory.

Example:

f = open("pythonex.txt", "w")
f.write("HELLO PYTHON!\nPython facilitates several functions to create, read, write, append, delete and close files.")
f.close()
 
f = open("pythonex.txt", "r")
b = f.read()
print b
f.close()

Continue Reading

Previous: What are Python functions
Next: What is python modules

Related Stories

What is Data Statistics And Analysis With Java And Python
9 min read
  • Learn Python

What is Data Statistics And Analysis With Java And Python

December 7, 2022
How do I get the date in Python
2 min read
  • Learn Python

How do I get the date in Python

December 7, 2022
What is an exception handler in Python
2 min read
  • Learn Python

What is an exception handler in Python

December 7, 2022

This AD Will support Us

Support Us

Coding Ustad LTD Support
Coding Ustad LTD Support

Coding Ustad LTD

Coding Ustad LTD
Coding Ustad LTD

Recent Posts

  • How to Install Node.js and npm on CentOS 7
  • How to Install Node.js and npm on CentOS 7
  • How to Install CentOS Web Panel (CWP) on CentOS 7
  • The Power of Marketing in Real Estate: A Guide to Boost Your Business
  • Vehicle Verification in Pakistan

Get free Hosting

Hostens.com - A home for your website

You may have missed

How to Install Node.js and npm on CentOS 7
4 min read
  • Coding Ustad LTD
  • Education for Everyone

How to Install Node.js and npm on CentOS 7

May 3, 2023
How to Install Node.js and npm on CentOS 7
4 min read
  • Coding Ustad LTD
  • Education for Everyone

How to Install Node.js and npm on CentOS 7

May 2, 2023
How to Install CentOS Web Panel (CWP) on CentOS 7
3 min read
  • Coding Ustad LTD
  • Education for Everyone

How to Install CentOS Web Panel (CWP) on CentOS 7

May 2, 2023
The Power of Marketing in Real Estate: A Guide to Boost Your Business https://thaikadar.com/secureinvestment/
2 min read
  • Article By Spoke Right
  • Blog By Spoke Right
  • Circular Byte Private Limited

The Power of Marketing in Real Estate: A Guide to Boost Your Business

January 30, 2023
  • Home
  • Our Services
  • Free Education
  • Spoke Right News
  • Earn Online
  • Classic Games by Spoke Right
  • Contact Us
Copyright © All rights reserved. | MoreNews by AF themes.