Introduction
Files are used to store data. It is used by many programming languages to store or use its content. Python also provides various functions for file handling in python. These functions are used for file operation in python.
Here in the article, we will do some python file handling exercises like create a text file in python, rename, remove and directory file operations in python.
Getting Started
The OS module in Python provides functions for interacting with the operating system. For any file operation in python(python file handling), it is required of importing os in python. You need to install the os library python, the following code helps to install the OS library.
pip install os-win
Python os module comes under Python’s standard utility modules. This module provides a portable way of using python operating system dependent functionality. The python os and python os path modules include many functions to interact with the file system.
Python File Handling
Create a Text File in Python
To create a new file in Python, use the open() method, with one of the following parameters:
- "x" - Create - will create a file, returns an error if the file exists.
- "a" - Append - will create a file if the specified file does not exist.
- "w" - Write - will create a file if the specified file does not exist.
The following code example creates a text file in python with some text.
# Program to show in file handling in python
_fileobject=open("D:\aricle.txt",'x')#open file w python
_fileobject.write("This is new file");#python write to text file
_fileobject.Close() #close file
Create a Text File in Python
Python list files in directory using the walk() function which returns a list of three items. It contains the name of the root directory, a list of the names of the subdirectories, and a list of the filenames in the current directory.
import os
for root, dirs, files in os.walk("."):
for filename in files:
print(filename)
Rename a File
To perform rename file operation in python, the rename() function is available in the python os module. The following code example shows how to rename a file, the name Article.txt is renamed by MyArticle.txt.
import os
os.rename("Article.txt","MyArticle.txt")
Python Remove File
Python delete files using the remove() function by specifying the name of the file as a parameter to remove() function. Following codes delete a file in python.
import os
os.remove("MyArticle.txt")
Python Directory Handling
Python Create Directory
Creating a directory in python is very easy as compare to other languages. The python mkdir() function is used to create new directories in python. The name of the directory is to be provided to the function as a parameter. The new directory will be created in the current directory.
import os
os.mkdir("Mydirectly")#python os mkdir
Python List Directory
Python list files in directory using python listdir() function. The following code listouts all the directries of current folder.
import os
os.mkdir("Mydocument")#python os mkdir
os.listdir();#python listdir
Python Active Directory
The getcwd python function is used to display the python active directory.
import os
os.getcwd()
Python Remove Directory
Python rddir() is used in the python delete folder. It takes a directory path as a parameter. The following example deletes a folder.
import os
os.rmdir("MyDocument")#python delete file if exists
Thanks