Table of contents
1.
Introduction
2.
Uploading the File and Reading it in CherryPy Python
2.1.
Requirements 
2.2.
Installation 
2.3.
Upload a File and Read its Contents
2.4.
HTML Code for Uploading a File from the System
2.5.
Code to Read File
3.
Frequently Asked Questions
3.1.
Is CherryPy a Python Library or framework?
3.2.
What Exactly is CherryPy?
3.3.
What are some of the characteristics of the Cherrypy Framework?
3.4.
Which Version of Python is required for CherryPy?
3.5.
What steps are necessary to upload a file and read its content using cherrypy?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Uploading the File and Reading it in CherryPy Python

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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

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

 

Installation

Also see, How to Check Python Version in CMD

Installation 

Use the following Command in the terminal to install Cherrypy:

Pip install cherrypy

 

Upload file and Read its content

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:

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())

Frequently Asked Questions

Is CherryPy a Python Library or framework?

CherryPy is a Python framework.

What Exactly is CherryPy?

CherryPy is a very famous Python framework. Web applications can be constructed or built faster and more reliably with CherryPy. It's also known as a web application library. Because it is based on object-oriented Python programming, it is used for its simplicity, resulting in minor source code in less time.

What are some of the characteristics of the Cherrypy Framework?

Cherrypy's primary capabilities include the ability to operate numerous HTTP servers (e.g. on various ports) at the same time. A robust configuration mechanism for developers and administrators. A versatile plugin system, Profiling, coverage, and testing are all built in.

Which Version of Python is required for CherryPy?

For successful installation of CherryPy, Python version 2.4 or higher is required.

What steps are necessary to upload a file and read its content using cherrypy?

Firstly Create any text file to read or utilize an existing file. The software makes use of the file Ninja.txt. Then create a user interface for uploading a file from the system. Lastly, create a Cherrypy program that reads a file and displays its contents.

Conclusion

In this article, we learned about Uploading the file and reading it in Cherrypy python, which includes the uploading of a code file and its working in CherryPy.

After reading about Uploading the file and reading it in Cherrypy python, are you not feeling excited to read/explore more articles on the topic of Ruby? Don't worry; Coding Ninjas has you covered. To learn, see Multithreading in Python, Descriptors in Python, and BorderLayout in Java.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! 

Do upvote our blogs if you find them helpful and engaging!

Live masterclass