杰瑞科技汇

Python如何保存文件?

Of course! Saving data to a file is a fundamental task in Python. The most common and flexible way to do this is by using the built-in open() function with a with statement.

Python如何保存文件?-图1
(图片来源网络,侵删)

Here’s a complete guide, starting with the basics and moving to more advanced scenarios.


The Core Concept: with open(...)

The best practice for handling files in Python is to use the with statement. It automatically takes care of closing the file for you, even if errors occur.

The basic syntax is:

with open(filename, mode) as file_object:
    # Do something with the file_object (e.g., read or write)
  • filename: A string representing the path to the file (e.g., my_data.txt).
  • mode: A string that specifies how you want to open the file. The most common modes are:
    • 'w': Write. Creates a new file or overwrites an existing one. Use this when you want to save fresh data.
    • 'a': Append. Adds data to the end of an existing file. Creates a new file if it doesn't exist. Use this when you want to add to existing data.
    • 'r': Read. Opens a file for reading. This is the default mode if you omit it.

Saving Text Data (Strings)

This is the most straightforward case. You'll use the file_object.write() method.

Python如何保存文件?-图2
(图片来源网络,侵删)

Example 1: Saving a Single String

Let's save a simple greeting to a file named greeting.txt.

# The data we want to save
message = "Hello, Python!\nThis is a test."
# The 'w' mode will create the file or overwrite it if it exists.
with open("greeting.txt", "w") as f:
    f.write(message)
print("File 'greeting.txt' has been written.")

After running this, a file named greeting.txt will be created with the following content:

Hello, Python!
This is a test.

Example 2: Appending to a File

Now, let's add more text to the existing greeting.txt file without deleting the old content.

# The data we want to add
additional_message = "\nAdding a new line."
# The 'a' mode will append to the end of the file.
with open("greeting.txt", "a") as f:
    f.write(additional_message)
print("New content has been appended to 'greeting.txt'.")

The content of greeting.txt will now be:

Hello, Python!
This is a test.
Adding a new line.

Saving Multiple Lines of Text (List of Strings)

If your data is in a list (e.g., from reading a CSV or log file), you can write each line at a time.

# A list of strings, where each string is a line
lines = [
    "Name, Age, City",
    "Alice, 30, New York",
    "Bob, 24, Los Angeles",
    "Charlie, 35, Chicago"
]
# The 'w' mode will create/overwrite the file.
with open("data.csv", "w") as f:
    # We can use a loop to write each line
    for line in lines:
        f.write(line + "\n") # Add a newline character to each line
print("File 'data.csv' has been written.")

A more efficient way to do this is using writelines():

lines = [
    "Name, Age, City\n",
    "Alice, 30, New York\n",
    "Bob, 24, Los Angeles\n",
    "Charlie, 35, Chicago\n"
]
with open("data_efficient.csv", "w") as f:
    f.writelines(lines) # writelines takes a list of strings and writes them

Saving Complex Data (Numbers, Lists, Dictionaries)

You cannot directly save complex Python objects like lists or dictionaries to a text file. If you try, you'll get an error because write() only accepts strings.

Incorrect Way (Will Cause an Error):

data = [1, 2, 3, "hello", {"name": "Dave"}]
with open("data.txt", "w") as f:
    f.write(data) # TypeError: write() argument must be str, not list

To handle this, you need to serialize your data, which means converting it into a format that can be easily stored and reconstructed. The two most common methods are JSON and Pickle.

Method A: Using the json Module (Recommended for most cases)

JSON (JavaScript Object Notation) is a lightweight, text-based format that is human-readable and understood by almost every programming language. It's perfect for configuration files and data interchange.

Saving to a JSON file:

import json
data = {
    "name": "John Doe",
    "age": 30,
    "is_student": False,
    "courses": ["History", "Math", "Art"],
    "address": {
        "street": "123 Main St",
        "city": "Anytown"
    }
}
# Use 'w' for write mode
with open("data.json", "w") as f:
    # The indent=4 argument makes the JSON file nicely formatted and readable
    json.dump(data, f, indent=4)
print("Data has been saved to data.json")

Content of data.json:

{
    "name": "John Doe",
    "age": 30,
    "is_student": false,
    "courses": [
        "History",
        "Math",
        "Art"
    ],
    "address": {
        "street": "123 Main St",
        "city": "Anytown"
    }
}

Method B: Using the pickle Module

pickle is a Python-specific module for serializing and de-serializing Python objects. It can handle almost any Python object, including custom classes, which json cannot.

Warning: Only unpickle data from sources you trust, as it can execute arbitrary code.

Saving to a pickle file:

import pickle
# A more complex object
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
p1 = Person("Alice", 28)
p2 = Person("Bob", 35)
data_to_save = [p1, p2]
# Use 'wb' for write-binary mode, as pickle creates binary files
with open("people.pkl", "wb") as f:
    pickle.dump(data_to_save, f)
print("Data has been pickled to people.pkl")

Saving Numerical Data (NumPy Arrays)

If you're working with scientific computing, you'll likely use the NumPy library. NumPy has its own optimized, efficient formats for saving arrays.

Saving a NumPy array to a binary file (.npy):

import numpy as np
# Create a sample NumPy array
data_array = np.random.rand(5, 5) # A 5x5 array of random numbers
# Save the array to a .npy file
np.save("my_array.npy", data_array)
print("NumPy array saved to my_array.npy")
# To load it back later:
# loaded_array = np.load("my_array.npy")
# print(loaded_array)

Saving multiple arrays to an uncompressed archive (.npz):

import numpy as np
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
# Save multiple arrays to a single .npz file
np.savez("arrays_archive.npz", first=array1, second=array2)
print("Multiple arrays saved to arrays_archive.npz")
# To load them back:
# archive = np.load("arrays_archive.npz")
# print(archive['first'])
# print(archive['second'])

Summary: Which Method to Use?

Data Type Recommended Method Why?
Simple Text with open(..., "w") and f.write() Simple, built-in, no imports needed.
List of Lines with open(..., "w") and f.writelines() Efficient for writing many lines at once.
Config / Structured Data json.dump() Human-readable, language-agnostic, standard format.
Python Objects / Custom Classes pickle.dump() Can save any Python object, but is Python-specific.
Numerical / Scientific Arrays np.save() / np.savez() Highly efficient and standard for NumPy users.
分享:
扫描分享到社交APP
上一篇
下一篇