Monthly Archives: October 2021

35.1 Create Database

Creating a Database To create a database in MySQL, use the “CREATE DATABASE” statement: Example create a database named “mydatabase”:import mysql.connectormydb = mysql.connector.connect(  host=”localhost”,  user=”yourusername“,  password=”yourpassword“)mycursor = mydb.cursor()mycursor.execute(“CREATE DATABASE mydatabase”)#If this page is executed with no error, you have successfully created a database. Output:C:\Users\My Name>python demo_mysql_create_db.py If the above code was executed with no errors, you have… Read More »

35. MySQL

Python can be used in database applications. One of the most popular databases is MySQL. MySQL Database To be able to experiment with the code examples in this tutorial, you should have MySQL installed on your computer. You can download a free MySQL database at https://www.mysql.com/downloads/. Install MySQL Driver Python needs a MySQL driver to access… Read More »

34.4 Delete File

Delete a File To delete a file, you must import the OS module, and run its os.remove() function: Example Remove the file “demofile.txt”:import osos.remove(“demofile.txt”) Check if File exist: To avoid getting an error, you might want to check if the file exists before you try to delete it: Example Check if file exists, then delete it:import osif os.path.exists(“demofile.txt”):  os.remove(“demofile.txt”)else:  print(“The file does not… Read More »

34.3 Write/Create File

Write to an Existing File To write to an existing file, you must add a parameter to the open() function: “a” – Append – will append to the end of the file “w” – Write – will overwrite any existing content Example Open the file “demofile2.txt” and append content to the file:f = open(“demofile2.txt”, “a”)f.write(“Now the file has more content!”)f.close() #open… Read More »

34.2 Read File

Open a File on the Server Assume we have the following file, located in the same folder as Python: demofile.txtHello! Welcome to demofile.txtThis file is for testing purposes.Good Luck! To open the file, use the built-in open() function. The open() function returns a file object, which has a read() method for reading the content of the file: Example f = open(“demofile.txt”, “r”)print(f.read()) Output:C:\Users\My… Read More »

34.1 File Open

File handling is an important part of any web application. Python has several functions for creating, reading, updating, and deleting files. File Handling The key function for working with files in Python is the open() function. The open() function takes two parameters; filename, and mode. There are four different methods (modes) for opening a file: “r” – Read – Default value. Opens… Read More »

33. String Formatting

To make sure a string will display as expected, we can format the result with the format() method. String format() The format() method allows you to format selected parts of a string. Sometimes there are parts of a text that you do not control, maybe they come from a database, or user input? To control such values, add placeholders… Read More »

32. User Input

User Input Python allows for user input. That means we are able to ask the user for input. The method is a bit different in Python 3.6 than Python 2.7. Python 3.6 uses the input() method. Python 2.7 uses the raw_input() method. The following example asks for the username, and when you entered the username, it gets printed on… Read More »

31. Try Except

The try block lets you test a block of code for errors. The except block lets you handle the error. The finally block lets you execute code, regardless of the result of the try- and except blocks. Exception Handling When an error occurs, or exception as we call it, Python will normally stop and generate an error message. These exceptions can… Read More »

30. PIP

What is PIP? PIP is a package manager for Python packages, or modules if you like. Note: If you have Python version 3.4 or later, PIP is included by default. What is a Package? A package contains all the files you need for a module. Modules are Python code libraries you can include in your project.… Read More »