Python File Write

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 and read the file after the appending:
f = open("demofile2.txt", "r")
print(f.read())

Continue reading Python File Write

Python 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 a file for reading, error if the file does not exist

"a" – Append – Opens a file for appending, creates the file if it does not exist

"w" – Write – Opens a file for writing, creates the file if it does not exist

"x" – Create – Creates the specified file, returns an error if the file exists

In addition you can specify if the file should be handled as binary or text mode

"t" – Text – Default value. Text mode

"b" – Binary – Binary mode (e.g. images)


Continue reading Python File Open

Python String Formatting

F-String was introduced in Python 3.6, and is now the preferred way of formatting strings.

Before Python 3.6 we had to use the format() method.


F-Strings

F-string allows you to format selected parts of a string.

To specify a string as an f-string, simply put an f in front of the string literal, like this:

Example

Create an f-string:

txt = f"The price is 49 dollars"
print(txt)

Continue reading Python String Formatting

Python 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. Continue reading Python User Input

Python Try Except

The try block lets you test a block of code for errors.

The except block lets you handle the error.

The else block lets you execute code when there is no 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 be handled using the try statement: Continue reading Python Try Except

Python 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.


Check if PIP is Installed

Navigate your command line to the location of Python’s script directory, and type the following:

Example

Check PIP version:

C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>pip --version

Continue reading Python PIP

Python RegEx

A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern.

RegEx can be used to check if a string contains the specified search pattern.


RegEx Module

Python has a built-in package called re, which can be used to work with Regular Expressions.

Import the re module:

import re

Continue reading Python RegEx

Python JSON

JSON is a syntax for storing and exchanging data.

JSON is text, written with JavaScript object notation.


JSON in Python

Python has a built-in package called json, which can be used to work with JSON data.

Example

Import the json module:

import json

Continue reading Python JSON

Python Math

Python has a set of built-in math functions, including an extensive math module, that allows you to perform mathematical tasks on numbers.


Built-in Math Functions

The min() and max() functions can be used to find the lowest or highest value in an iterable:

Example

x = min(5, 10, 25)
y = max(5, 10, 25)

print(x)
print(y)

The abs() function returns the absolute (positive) value of the specified number: Continue reading Python Math

Python Datetime

Python Dates

A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects.

Example

Import the datetime module and display the current date:

import datetime

x = datetime.datetime.now()
print(x)

Continue reading Python Datetime