Data Structure and Algorithm using Python

PROGRAMMING ESSENTIALS IN PYTHON

File Manipulation

Peter Lo

File

There are two separate types of files that Python handles: Binary file Text files.

Knowing the difference between the two is important because of how they are handled.

Programming Essentials in Python @ Peter Lo 2022

2

Binary File

Most files that you use in your computer are binary files. For example, Microsoft Word .doc file is a binary file, even if it has text in it.

Other examples of binary files include:

Image files including .jpg, .png, .bmp, .gif, etc.

Database files including .mdb, .frm, and .sqlite

Documents including .doc, .xls, .pdf, and others.

That's because these files all have requirements for special handling and require a specific type of software to open it. For example, you need Excel to open an .xls file

Programming Essentials in Python @ Peter Lo 2022

3

Text File

A text file has no specific encoding and can be opened by a standard text editor without any special handling.

Every text file must adhere to a set of rules:

Text files have to be readable as is. They can contain a lot of special encoding, especially in HTML or other markup languages, but you'll still be able to tell what it says

Data in a text file is organized by lines. In most cases, each line is a distinct element, whether it's a line of instruction or a command.

Text files have some unseen character at the end of each line which lets the text editor know that there should be a new line. In Python, it is denoted by the "\n".

Programming Essentials in Python @ Peter Lo 2022

4

Comma Separated Value (CSV) Text File

A CSV file is a comma separated values file commonly used by spreadsheet programs such as Microsoft Excel.

It contains plain text data sets separated by commas with each new line in the CSV file representing a new database row and each database row consisting of one or more fields separated by a comma.

Programming Essentials in Python @ Peter Lo 2022

5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download