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?
You will need to use a python in the build method (function) Open
to get a file object and create the file. The file object has a function and attributes to write and read etc.
Syntax:
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")
Modes
w
write mode (if the file doesn’t exist create it and open it in write mode)r
read modea
append 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 moder+
open an existing file in read+write modea+
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")
Output :
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")
Output :
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 os.path.exists
function:
import os.path os.path.exists(file_path)
It will returnTrue
for both files or directories but you can instead use if the file is specified.
os.path.isfile(file_path)
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
Do comment if you have any doubts or suggestions on this topic.
Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6Python 3.7
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.
How to separate text? I want to read text from file that on each line I have file name to create.
1
2
3
ect.
Using split() method
import os.path
filePath = “c://Python_Tools//List.txt”
with open (filePath, “r”) as myfile:
data = myfile.readlines()
for line in data:
fn = str(data) + “.txt”
f = open(fn, “w”)
is not working.
What error are you getting?
it’s not working.
FileNotFoundError: [Errno 2] No such file or directory: “[‘1,\\n’, ‘2,\\n’, ‘3,’].txt”
also text file have name [‘1,2,3’].txt
can you help?
You have to separate text and then create file.
in text file “hello.txt” I have text: 1,2,3. How to create list file: 1.txt, 2.txt, 3.txt?
Use for loop and range() Function – https://tutorial.eyehunts.com/python/python-for-loops-tutorial-example/
for x in range(3):
fn = str(x)+".txt"
f = open(fn, "w")