Creating a file in python is very easy. With python inbuilt function you can create a text file, PDF file, images file (jpeg, png..), etc. In this tutorial, you will learn the basics of creating files and functions with examples. To Python Create File you must rely on the built-in open() function where the Open function is an inbuilt method.
How to Python Create File?
Where file_obj is a variable to hold the file object. The mode argument is required ‘w’ because the default value of ‘r’ will be assumed if it is omitted. Where ‘w ‘value stands for write mode.
You have to use any one of mode form w, a, w+ and a+ to create a file.
#create file file_obj = open("filename", "mode")
wwrite mode (if the file doesn’t exist create it and open it in write mode)
aappend mode (if the file doesn’t exist create it and open it in append mode)
w+create a file – if it doesn’t exist and open it in write mode
r+open an existing file in read+write mode
a+create a file – if it doesn’t exist and open it in append mode
Python Create a text file example
We are using w mode to create a file and writing a text in the file.
f = open("cFile.txt", "w") f.write(" Created file")
Creating more files
In this example, we are creating pdf and image files.
pf = open("picFile.png", "w") jf = open("imgFile.jpg", "w") pdff = open("pdfFile.pdf", "w")
QA: How to Python check if the file exists and Create if the file not there?
Its simple question can ask in an interview “How python creates a file if not exists“?
You can do it with
import os.path os.path.exists(file_path)
It will return
True for both files or directories but you can instead use if the file is specified.
Using a Write mode “w” or “w+” will create a file if not exists in Python.
Or use this code first check file exists or not, then do create it.
import os.path file_exists = os.path.isfile(filename) if file_exists: # do something else: # do something else
Python File Handling Quiz
0 of 10 questions completed
Python File Handling to test your knowledge – ( Create, Open, Update, delete and more about in Python)
It’s for Beginners, Advanced and Experienced Programmers.
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
0 of 10 questions answered correctly
Time has elapsed
You have reached 0 of 0 points, (0)
|Table is loading|
|No data available|
Question 1 of 10
Which of the following command is used to open a file “c:\textFile.txt” in read-mode only?Correct
Question 2 of 10
Which functions use to check if a file exists?
File name = “logo”Correct
Question 3 of 10
How do you insert something on a new line in a file?Correct
Question 4 of 10
Appending to a file means adding extra data into the file.Correct
Question 5 of 10
What is the last action that must be performed on a file?Correct
Question 6 of 10
What is the data type of data read from a file?Correct
Question 7 of 10
Can you create a file without using file modes in Python?Correct
Question 8 of 10
Reading from a file often involves using…Correct
Question 9 of 10
Which of the following statements are true regarding the opening modes of a file?Correct
Question 10 of 10
Which of the following commands option right to read the entire contents of a file as a string using the file object
Do comment if you have any doubts or suggestions on this topic.
Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
All How to create a file in Python Examples (and if not exists) are in Python 3, so it may change its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.