Introduction
In this blog, we will learn how to Upload the file and read it in CherryPy python. It seems easy to do so, but still, it has a few steps which we have to keep in mind while Uploading the file and reading it in CherryPy python, which are explained completely below. So let's start.

Uploading the File and Reading it in CherryPy Python
CherryPy is a Python web framework that provides a user-friendly interface to the HTTP protocol for Python developers. It's also known as a web application library. It enables developers to create web apps in the same manner that they would create any other object-oriented Programming language. As a result, less source code is created in less time.
Requirements
The fundamental requirements for installing the CherryPy framework are as follows:
- Python version 2.4 or higher
- CherryPy 3.0

Also see, How to Check Python Version in CMD
Installation
Use the following Command in the terminal to install Cherrypy:
Pip install cherrypy

Upload a File and Read its Contents
The following are the steps necessary to upload a file and read its content using Cherrypy:
- Create any text file to read or utilize an existing file.
- Create a user interface for uploading a file from the system.
- Create a Cherrypy program that reads a file and displays its contents.
HTML Code for Uploading a File from the System
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Coding Ninjas</title>
<meta name="google-site-verification" content="ASe_HQRsu8j61tGt95RROjXi07M380nMuoJ0Mu9rytk">
<meta name="title" content="Coding Ninjas">
<meta property="og:title" content="Coding Ninjas">
<meta name="description" content="Best Programming Institute in India">
<meta property="og:description" content="Best Programming Institute in India">
<head>
<style>
body {
background-color: rgb(199, 107, 197);
}
p {
font: 1em sans-serif;
font-style: normal;
}
body {
background-image: url("./cn.png");
background-repeat: no-repeat;
background-position: right top;
}
</style>
<body>
<centre>
<h1>Welcome to Coding Ninjas</h1>
</centre>
</body>
</html>
Output:

Code to Read File
# import files
import random
import string
import cherrypy
# function to read file content
def readf(filename):
file = open(filename)
read = file.read()
return read
class Root(object):
@cherrypy.expose
def index(self):
return """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Coding Ninjas</title>
<meta name="google-site-verification" content="ASe_HQRsu8j61tGt95RROjXi07M380nMuoJ0Mu9rytk">
<meta name="title" content="Coding Ninjas">
<meta property="og:title" content="Coding Ninjas">
<meta name="description" content="Best Programming Institute in India">
<meta property="og:description" content="Best Programming Institute in India">
<style>
body {
background-color: rgb(199, 107, 197);
}
p {
font: 1em sans-serif;
font-style: normal;
}
body {
background-image: url("./cn.png");
background-repeat: no-repeat;
background-position: right top;
}
</style>
<head>
<body>
<centre>
<h1>Welcome to Coding Ninjas</h1>
</centre>
</body>
</html>"""
@cherrypy.expose
def store(self, myFile):
# read the uploaded file
f = readf(myFile)
return f
if __name__=="__main__":
# set port address to 8089
cherrypy.config.update({'server.socket_port': 8089})
cherrypy.quickstart(Root())



