What is Python file handling?
Python file handling is an example of how to create, open, read, write, and delete files in Python. Read data from files (EXCELSheet, DOC file, text file, CV, etc.) or Add. It’s a file with a program.
Python file handling
The main function for handling files (folders) in Python is the Open () function.
The open() function first takes two arguments filename and another mode.
The file name is the file path with the same name and mode as the required action on the file.
File processing mode
- W write mode – If the file does not exist, create it and open it in write mode. The flow is in the start file.
- R Read mode -(only reads files) is the default setting in the OPEN function. The flow is in the start file.
- A Attach mode (if the file does not exist, create it and open it in attach mode). The stream is in the end file.
- W + Create a file – if it doesn’t exist, and open it in write mode.
- **r+** Opens an existing file in read + write mode.
- A +- If it doesn’t exist, turn it on in attach mode.
Create a file
We use W mode To create the file, do the following. If the file does not exist, create and open it.
f = open("cFile.txt", "w")
f.write(" Created file"
Copy the code
Output:
Create more files:
In this case, we are creating PDF and image files.
PF = open (" picFile. PNG ", "W") open JF = (" imgFile. JPG ", "W") PDFF = open (" pdfFile. PDF ", "W")Copy the code
Output:
How does Python check if a file exists and create one that doesn’t?
You can use the os.path.exists function:
Import os. path OS. path. Exists (file path)Copy the code
The returned True is for both files or directories, but can be used if the file is specified.
Os.path.isfile (File path)Copy the code
Use write mode “W” or “w +” to create a file if one does not exist in Python.
Or use this code to first check if the file exists and then create it.
Import os. path file exists = os. path. Isfile(filename) If the file exists: # do something else: # do something elseCopy the code
Written to the file
In the same folder as Python, the empty ** “testfile.txt” ** file. (If the file does not exist, create it)
Now write the file. ‘
w
‘- Write mode
F = Open (" testfile.txt ", "W") f. Write (" Hello file ")Copy the code
Output:
Write the file. ‘
a
‘- Attach mode
F = Open (" testfile.txt ", "A") f. Write (" attach mode ")Copy the code
** Output: ** In the text file, the text is added last. You can print text in the () console.
Read file & Open file
There are ** “testfile.txt” ** files in the same folder as Python. Read files using ‘R’ – read mode
Read all the data in the file print () console.
Testfile.txt Hello world this is the text in the file. f = open("testFile.txt", "r") print(f.read())Copy the code
Output: Hello world
Only parts of the file are read
Examples of reading and printing () with only limited data, you can also specify how many characters to return:
f = open("testFile.txt", "r")
print(f.read(5))
Copy the code
** Hello
Read line sample
Using the readline() method:
F = open (" testfile.txt ", "r") print(f. Read wire ())Copy the code
** Output: ** Hello world
Delete (delete) files
Python deletion methods for files and folders.
-
Os.remove ()- Remove (delete) files
The path
.
-
Os.rmdir () will remove an empty directory.
-
Shutil.rmtree () deletes a directory and all its contents.
First, check if a file or folder exists, and then delete only that file.
Assume the project “cfile.txt.” has been submitted. Then execute:
import os
if os.path.exists("cFile.txt"):
os.remove("cFile.txt")
else:
print('File does not exists')
Copy the code
If the file does not exist, the output will print () “File does not exist”
To delete an entire folder, you must use the os.rmdir() method. It will delete the only empty folder. For complete deletion of files in folders, use shutil.rmtree().
Import OS OS. Rendell (" FolderName ")Copy the code
Deleting multiple files
To delete multiple files, simply walk through the list of files and use the os.rmdir() function above.
To delete the folder that contains all the files you want to delete, you must import the Shutil package.
You can then delete the folder as follows.
import shutil
shutil.rmtree('my_folder')
Copy the code
If you want to learn more about Python, the author provides a platform for you to communicate with others who have the same interests as you