How to Create a file in Python | Python File Handling

Опубликовано: 05 Октябрь 2024
на канале: Parag Dhawan
562
20

How to Create a file in Python | Python File Handling
A file is some information or data which stays in the computer storage devices. Python gives you easy ways to manipulate these files. Generally, we divide files into two categories, text file, and binary file. Text files are simple text whereas binary files contain binary data which is only readable by a computer.

How to Create a File
fobj = open("new.txt","x")
for i in range(0,10):
fobj.write(f" line no is {i} \n")
fobj.close()

Various modes of opening a file
“r” Opens the file in read-only mode and this is the default mode.
“w” Opens the file for writing. If a file already exists, then it’ll get overwritten. If the file does not exist, then
it creates a new file.
“a” Opens the file for appending data at the end of the file automatically. If the file does not exist it creates
a new file.
“r+” Opens the file for both reading and writing.
“w+” Opens the file for reading and writing. If the file does not exist it creates a new file. If a file already
exists then it will get overwritten.
“a+” Opens the file for reading and appending. If a file already exists, the data is appended. If the file does
not exist it creates a new file.
“x” Creates a new file. If the file already exists, the operation fails.
“rb” Opens the binary file in read-only mode.
“wb” Opens the file for writing the data in binary format.
“rb+” Opens the file for both reading and writing in binary format.

=============================================================================
Link for Tutorial Series

Jupyter Notebook Tutorial Series:-
   • How To Open Jupyter Notebook in Windows  

Python Tutorial Series:-
   • Introduction to Python | Python Appli...  

Python Assignments and Objective Questions:-
   • Objective Questions Python - 1  

Tech. Videos By Parag Dhawan;-
   • Template Matching Using OpenCV (Pytho...  

Object-Oriented Programming in Python-
   • How to Create Class and Object in Python  

=============================================================================
Feel free to connect and ask your queries:-

Linkedin:-   / parag-dhawan  
Youtube:-    / paragdhawan  
Facebook Page:- http://fb.me/dhawanparag
Instagram: -   / paragdhawan  
Twitter:-   / dhawan_parag  
GitHub:- https://github.com/paragdhawan/
Facebook Profile:- https://www.facebook.com/profile.php?...

=============================================================================
Show your support by Subscribing to the channel:-
https://www.youtube.com/c/ParagDhawan...
=============================================================================
#ParagDhawan
#PythonCrashCourse
#Python
#PythonTutorialForBeginners
#PythonForDataScience
#PythonProgramming
#PythonProgrammingLanguage
#PythonTutorial
#PythonCode
#Python3
============================================================
How to Record Your Screen and make a tutorial video or demo video: -
   • How to Record Screen and Edit Video U...  
============================================================