🎁Learn Python in 10 Days: Day7

johnjava

William

Posted on September 24, 2024

🎁Learn Python in 10 Days: Day7

Today, we're continuing our 10-day journey to learn Python, kicking off Day 7's lesson. If you haven't checked out Day 1 yet, you can find it here: 🎁Learn Python in 10 Days: Day 1

Day 7: File Operations

  1. File Encoding

Since computers can only recognize 0s and 1s, we use encoding techniques (like a codebook) to translate content into 0s and 1s to store it, allowing computers to recognize text files.

Encoding Techniques: These are the rules for translating content into binary and vice versa.

There are several encodings available in computers: UTF-8, GBK, Big5, and others. Different encodings translate the content into binary in different ways.

UTF-8 is the most commonly used encoding format worldwide. Unless there's a specific need, you should always use UTF-8 for file encoding.

  1. Reading Files

File Concept: Data in memory will disappear once the computer is turned off. To save data long-term, we use devices like hard drives, CDs, and USB drives. The concept of "files" was introduced to make data management and retrieval easier.

An article, a video clip, an executable program, they can all be stored as a file with a filename. The operating system manages disk data in units of files. Generally, files can be categorized into text files, video files, audio files, image files, executable files, etc.

Basic File Operations: Opening a file, reading/writing a file, and closing a file (you may just open and close it without any additional operations).

open() Function:
In Python, the open function is used to open an existing file or create a new one. Here's the syntax:

open(name, mode, encoding)
Enter fullscreen mode Exit fullscreen mode
  • name: The string name of the target file to be opened (it can include the file's path).
  • mode: The mode in which the file should be opened: read-only, write, append, etc.
  • encoding: The encoding format (default is UTF-8).

Example:

f = open("python.txt", "r", encoding="UTF-8")
# The encoding parameter is not the third one, so use a keyword argument to specify it directly.
print(type(f))  # <class '_io.TextIOWrapper'>
Enter fullscreen mode Exit fullscreen mode

Here, f is the file object from the open function, which is a special type in Python, having attributes and methods that can be used.

Modes and Descriptions:

  • r: Opens the file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
  • w: Opens a file for writing only. If the file exists, open it and start editing from the beginning, erasing the original content. If the file does not exist, create a new one.
  • a: Opens a file for appending. If the file exists, new content will be added to the end of the existing content. If the file does not exist, create a new one to write into.

read() Method:

file_object.read(num)
Enter fullscreen mode Exit fullscreen mode
  • num specifies the length of data to read from the file (in characters). If num is not passed, it reads all the data in the file.

readlines() Method:

readlines reads the entire content of the file at once, returning a list where each line is an element.

Example:

f = open("python.txt", "r", encoding="UTF-8")
# content = f.read()
# print(content)  # Content in the text: Hello
# f.close()
content = f.readlines()
print(content)  # ['Hello\n', 'World\n', 'is big']
f.close()
Enter fullscreen mode Exit fullscreen mode

readline() Method: Reads one line at a time.

Example:

# Read one line at a time: readline()
f = open("python.txt", "r", encoding="UTF-8")
content = f.readline()
print(f"First line: {content}")
content = f.readline()
print(f"Second line: {content}")
f.close()
Enter fullscreen mode Exit fullscreen mode

Reading File Lines with a For Loop:

Example:

# Read file lines with a for loop
for line in open("python.txt", "r", encoding="UTF-8"):
    print(line)
# Each 'line' temporary variable holds one line of data from the file
Enter fullscreen mode Exit fullscreen mode

Make sure to close the file object after you're done, to release the file from being used by the Python program. If not closed, and the program hasn't ended, the file will remain occupied by the Python process.

with open Syntax:

Example:

with open("python.txt", "r", encoding="UTF-8") as f:
    f.readlines()
# Using 'with open', the file is automatically closed after the block of code is executed.
Enter fullscreen mode Exit fullscreen mode

Example: Counting "hello" in a document:

Method 1:

f = open("test.txt", "r", encoding="utf-8")
content = f.read()
count = content.count("hello")
print(count)
f.close()
Enter fullscreen mode Exit fullscreen mode

Method 2:

f = open("test.txt", "r", encoding="utf-8")
count = 0
for line in f:
    line = line.replace("\n", "")
    words = line.split(" ")
    for word in words:
        if word == "hello":
            count += 1
print(count)
f.close()
Enter fullscreen mode Exit fullscreen mode
  1. Writing to Files

File Writing:

# Open file
f = open(name, "w", encoding="UTF-8")

# Write to file
f.write(content)

# Flush content
f.flush()
Enter fullscreen mode Exit fullscreen mode

Note:

  1. Directly calling write doesn’t immediately write content to the file but stores it in the program's memory, called a buffer.
  2. Calling flush writes the content to the file.
  3. This is to avoid frequent hard drive operations that can degrade performance.

Example:

import time
f = open("test.txt", "w")
f.write("Hello, world!")
f.flush()
time.sleep(50000)
f.close()  # The 'close' method includes the flush functionality
# If the file already exists, its original content will be cleared
f = open("test.txt", "w")
f.write("Haha")
f.close()
Enter fullscreen mode Exit fullscreen mode
  1. Appending to Files

Syntax:

# Open file
f = open(name, "a", encoding="UTF-8")

# Write to file
f.write(content)

# Flush content
f.flush()
Enter fullscreen mode Exit fullscreen mode

Example:

f = open("append_test.txt", "a", encoding="utf-8")
f.write("Keep the passion")
f.close()
f = open("append_test.txt", "a", encoding="utf-8")
f.write(", and explore the world")
f.close()
# Result: "Keep the passion, and explore the world"
Enter fullscreen mode Exit fullscreen mode
  1. File Operations (Comprehensive Case)

Example: Backup a file, remove marked data:

# Backup case: A bill recording expenses and income is backed up to 'csdn.txt.bak', removing test data marked as "测试"
fr = open("csdn.txt", "r", encoding="utf-8")
fw = open("csdn.txt.bak", "w", encoding="utf-8")
for line in fr:
    line = line.strip()
    if line.split(",")[4] == "test":
        continue
    fw.write(line)
    # Since strip() was used, manually write newline characters
    fw.write("\n")
fr.close()
fw.close()
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
johnjava
William

Posted on September 24, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

🎁Learn Python in 10 Days: Day7
python 🎁Learn Python in 10 Days: Day7

September 24, 2024