Ổ đĩa mạng đường dẫn Python

Tôi đang cố gắng phát triển cảm biến tùy chỉnh bằng cảm biến tập lệnh python nâng cao. Tập lệnh sẽ liệt kê các thư mục và tệp của thư mục con nằm trong thư mục windows được chia sẻ trên mạng. Thư mục được ánh xạ dưới dạng ổ đĩa mạng trong máy chủ prtg

Tập lệnh hoạt động chính xác khi được thực thi từ cửa sổ cmd trên máy chủ prtg nhưng khi được thực thi từ cảm biến, nó không thể tìm/truy cập thư mục dùng chung mạng

Tôi đã thử sử dụng ổ đĩa được ánh xạ và cả các đường dẫn chú thích nhưng không gặp may

Tôi đã định cấu hình cảm biến để chạy trên đầu dò cục bộ bằng người dùng Windows Quản trị viên [giống như cmd sử dụng]

dưới đây bạn có thể tìm thấy tập lệnh cơ sở tôi đang sử dụng

# -*- coding: utf-8 -*-

import json
import sys
import os
from prtg.sensor.result import CustomSensorResult
from prtg.sensor.units import ValueUnit

if __name__ == "__main__":
    try:
        # data = json.loads[sys.argv[1]]


        path_temp = "\\\\HOST\\folder\\path"
        #path_temp = "Z:\\folder\\path" 
        #print[path_temp+" "+str[os.path.exists[path_temp]]+" "+os.getlogin[]]
        
        
        csr = CustomSensorResult[text=path_temp+" "+str[os.path.exists[path_temp]]+" "+os.getlogin[]]



        print[csr.json_result]



    except Exception as e:
        csr = CustomSensorResult[text="Python Script execution error"]
        csr.error = "Python Script execution error: %s" % str[e]
        print[csr.json_result]

Kết quả từ cmd

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}

Kết quả từ cảm biến PRTG

{"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}

sự khác biệt là khi chạy từ prtg hệ điều hành. con đường. tồn tại [path_temp] phân giải thành Sai nhưng không đúng

PHIÊN BẢN PRTG. Phiên bản Python mới nhất. Cài đặt PRTG. Windows Server 2016

Cảm ơn bạn trước

tập tin giám sát tập tin ổ đĩa ánh xạ mạng thư mục chia sẻ mạng python python-script-advanced-sensor

Biến là một cách tốt để lưu trữ dữ liệu trong khi chương trình của bạn đang chạy, nhưng nếu bạn muốn dữ liệu của mình tồn tại ngay cả sau khi chương trình của bạn kết thúc, bạn cần lưu nó vào một tệp. Bạn có thể coi nội dung của tệp dưới dạng một giá trị chuỗi đơn lẻ, có thể có kích thước hàng gigabyte. Trong chương này, bạn sẽ học cách sử dụng Python để tạo, đọc và lưu tệp trên ổ cứng

Tệp và Đường dẫn tệp

Một tệp có hai thuộc tính chính. a tên tệp [thường được viết bằng một từ] và đường dẫn . Đường dẫn chỉ định vị trí của tệp trên máy tính. Ví dụ: có một tệp trên máy tính xách tay Windows 7 của tôi có tên tệp project. docx trong đường dẫn C. \Users\asweigart\Documents . Phần tên tệp sau dấu chấm cuối cùng được gọi là phần mở rộng của tệp và cho bạn biết loại tệp. dự án. docx là tài liệu Word và Người dùng , asweigart, and Documents all refer to folders [also called directories]. Folders can contain files and other folders. For example, dự án. docx nằm trong thư mục Tài liệu , nằm trong asweigartasweigart . hiển thị tổ chức thư mục này. asweigart . hiển thị tổ chức thư mục này. asweigart . hiển thị tổ chức thư mục này. asweigart . hiển thị tổ chức thư mục này. asweigart . hiển thị tổ chức thư mục này. asweigart . hiển thị tổ chức thư mục này. asweigart . hiển thị tổ chức thư mục này. asweigart . hiển thị tổ chức thư mục này. asweigart . hiển thị tổ chức thư mục này. asweigart . hiển thị tổ chức thư mục này. asweigart . hiển thị tổ chức thư mục này. asweigart . hiển thị tổ chức thư mục này. asweigart . hiển thị tổ chức thư mục này. asweigart . hiển thị tổ chức thư mục này. asweigart . hiển thị tổ chức thư mục này. asweigart . hiển thị tổ chức thư mục này. asweigart . hiển thị tổ chức thư mục này. asweigart . hiển thị tổ chức thư mục này. folder, which is inside the Users folder. shows this folder organization.

Hình 8-1. Một tệp trong hệ thống phân cấp các thư mục

Cái C. \ một phần của đường dẫn là thư mục gốc , chứa tất cả các thư mục khác. Trên Windows, thư mục gốc có tên C. \ và còn được gọi là C. lái xe . Trên OS X và Linux, thư mục gốc là / . Trong cuốn sách này, tôi sẽ sử dụng thư mục gốc kiểu Windows, C. \ . Nếu bạn đang nhập các ví dụ shell tương tác trên OS X hoặc Linux, hãy nhập

>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
0 để thay thế.

bổ sung, chẳng hạn như ổ đĩa DVD hoặc ổ USB, sẽ xuất hiện khác nhau trên các hệ điều hành khác nhau. Trên Windows, chúng xuất hiện dưới dạng ổ đĩa gốc mới, có ký tự, chẳng hạn như D. \ hoặc E. \ . Trên OS X, chúng xuất hiện dưới dạng các thư mục mới trong thư mục /Tập . Trên Linux, chúng xuất hiện dưới dạng các thư mục mới trong thư mục /mnt [“mount”]. Cũng lưu ý rằng mặc dù tên thư mục và tên tệp không phân biệt chữ hoa chữ thường trên Windows và OS X, nhưng chúng lại phân biệt chữ hoa chữ thường trên Linux.

Dấu gạch chéo ngược trên Windows và Dấu gạch chéo ngược trên OS X và Linux

Trên Windows, đường dẫn được viết bằng dấu gạch chéo ngược [ \ ] làm dấu phân cách giữa các tên thư mục. Tuy nhiên, OS X và Linux sử dụng dấu gạch chéo lên [ / ] làm dấu phân cách đường dẫn của chúng. Nếu bạn muốn chương trình của mình hoạt động trên tất cả các hệ điều hành, bạn sẽ phải viết tập lệnh Python để xử lý cả hai trường hợp.

May mắn thay, điều này rất đơn giản để thực hiện với hàm

>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
1. Nếu bạn chuyển nó các giá trị chuỗi của tên tệp và thư mục riêng lẻ trong đường dẫn của bạn, thì
>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
1 sẽ trả về một chuỗi có đường dẫn tệp bằng cách sử dụng dấu tách đường dẫn chính xác. Nhập thông tin sau vào vỏ tương tác

>>> import os
>>> os.path.join['usr', 'bin', 'spam']
'usr\\bin\\spam'

Tôi đang chạy các ví dụ shell tương tác này trên Windows, vì vậy,

>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
3 đã trả về
>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
4. [Lưu ý rằng các dấu gạch chéo ngược được nhân đôi vì mỗi dấu gạch chéo ngược cần được thoát bởi một ký tự gạch chéo ngược khác. ] Nếu tôi đã gọi hàm này trên OS X hoặc Linux, chuỗi sẽ là
>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
5

hệ điều hành. con đường. hàm join[] rất hữu ích nếu bạn cần tạo chuỗi cho tên tệp. Các chuỗi này sẽ được chuyển đến một số chức năng liên quan đến tệp được giới thiệu trong chương này. Ví dụ: ví dụ sau nối tên từ danh sách tên tệp vào cuối tên thư mục

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
0

Thư mục làm việc hiện tại

Mọi chương trình chạy trên máy tính của bạn đều có thư mục làm việc hiện tại hoặc cwd. Any filenames or paths that do not begin with the root folder are assumed to be under the current working directory. You can get the current working directory as a string value with the

>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
6 function and change it with
>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
7. Enter the following into the interactive shell:

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
3

Ở đây, thư mục làm việc hiện tại được đặt thành C. \Python34 , vì vậy tên tệp dự án. docx đề cập đến C. \Python34\dự án. docx . Khi chúng tôi thay đổi thư mục làm việc hiện tại thành C. \Windows , dự án. docx được hiểu là C. \Windows\dự án. docx .

Python sẽ hiển thị lỗi nếu bạn cố thay đổi thư mục không tồn tại

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
4

Ghi chú

Mặc dù thư mục là tên thư mục hiện đại hơn, nhưng hãy lưu ý rằng thư mục làm việc hiện tại [hoặc chỉ . ] is the standard term, not current working folder.

tuyệt đối so với. Đường dẫn tương đối

Có hai cách để chỉ định đường dẫn tệp

  • Một đường dẫn tuyệt đối , luôn bắt đầu bằng thư mục gốc

  • A đường dẫn tương đối , liên quan đến thư mục làm việc hiện tại của chương trình

Ngoài ra còn có chấm [

>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
8] và chấm-chấm [
>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
9] folders. These are not real folders but special names that can be used in a path. A single period [“dot”] for a folder name is shorthand for “this directory.” Two periods [“dot-dot”] means “the parent folder.”

là một ví dụ về một số thư mục và tệp. Khi thư mục làm việc hiện tại được đặt thành C. \bacon , các đường dẫn tương đối cho các thư mục và tệp khác được đặt như trong hình.

Hình 8-2. Đường dẫn tương đối cho các thư mục và tệp trong thư mục làm việc C. \bacon

Cái . \ ở đầu đường dẫn tương đối là tùy chọn. Ví dụ: . \Thư rác. txtthư rác. txt đề cập đến cùng một tệp.

Tạo thư mục mới với os. makedirs[]

Các chương trình của bạn có thể tạo các thư mục [directory] mới với hàm

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
70. Nhập thông tin sau vào vỏ tương tác

>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']

Điều này sẽ không chỉ tạo ra C. \delicious nhưng cũng có thư mục walnut bên trong C. \delicious và thư mục waffles bên trong C. \ngon\quả óc chó . Nghĩa là,

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
70 sẽ tạo bất kỳ thư mục trung gian cần thiết nào để đảm bảo rằng đường dẫn đầy đủ tồn tại. hiển thị hệ thống phân cấp thư mục này.

Hình 8-3. Kết quả của

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
72

hệ điều hành. đường dẫn Mô-đun

Mô-đun

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
73 chứa nhiều chức năng hữu ích liên quan đến tên tệp và đường dẫn tệp. Chẳng hạn, bạn đã sử dụng
>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
1 để xây dựng đường dẫn theo cách sẽ hoạt động trên mọi hệ điều hành. Vì
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
73 là một mô-đun bên trong mô-đun
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
76, nên bạn có thể nhập mô-đun đó bằng cách chạy
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
77. Bất cứ khi nào chương trình của bạn cần làm việc với tệp, thư mục hoặc đường dẫn tệp, bạn có thể tham khảo các ví dụ ngắn trong phần này. Tài liệu đầy đủ cho mô-đun
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
73 có trên trang web Python tại http. // tài liệu. con trăn. org/3/library/os. con đường. html .

Ghi chú

Hầu hết các ví dụ tiếp theo trong phần này sẽ yêu cầu mô-đun

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
76 , vì vậy hãy nhớ nhập mô-đun đó vào đầu bất kỳ tập lệnh nào của bạn. . Nếu không, bạn sẽ nhận được
{"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
80 thông báo lỗi.

Xử lý đường dẫn tuyệt đối và tương đối

Mô-đun

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
73 cung cấp các hàm để trả về đường dẫn tuyệt đối của đường dẫn tương đối và để kiểm tra xem đường dẫn đã cho có phải là đường dẫn tuyệt đối hay không

  • Gọi

    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    82
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    83
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    84 sẽ trả về một chuỗi đường dẫn tuyệt đối của đối số. Đây là một cách dễ dàng để chuyển đổi một đường dẫn tương đối thành một đường dẫn tuyệt đối.

  • Gọi

    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    85
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    83
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    84 sẽ trả về
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    88 nếu đối số là đường dẫn tuyệt đối và
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    89 nếu đó là đường dẫn tương đối.

  • Gọi

    >>> import os
    >>> os.makedirs['C:\\delicious\\walnut\\waffles']
    80
    >>> import os
    >>> os.makedirs['C:\\delicious\\walnut\\waffles']
    81
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    84 sẽ trả về một chuỗi đường dẫn tương đối từ
    >>> import os
    >>> os.makedirs['C:\\delicious\\walnut\\waffles']
    83
    path to
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    83
    . If ______383 không được cung cấp, thư mục làm việc hiện tại được sử dụng làm đường dẫn bắt đầu.

Hãy thử các chức năng này trong trình bao tương tác

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
7

C. \Python34 là thư mục làm việc khi

>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
86 được gọi, thư mục “một dấu chấm” đại diện cho đường dẫn tuyệt đối
>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
87.

Ghi chú

Vì hệ thống của bạn có thể chứa các tệp và thư mục khác với hệ thống của tôi, nên bạn sẽ không thể theo dõi chính xác mọi ví dụ trong chương này. Tuy nhiên, hãy cố gắng làm theo bằng cách sử dụng các thư mục tồn tại trên máy tính của bạn

Nhập các cuộc gọi sau tới

>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
88 vào trình bao tương tác

{"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
8

Gọi

>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
89
{"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
83
{"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
84 sẽ trả về một chuỗi chứa mọi thứ xuất hiện trước dấu gạch chéo cuối cùng trong đối số
{"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
83. Gọi
>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
33
{"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
83
{"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
84 sẽ trả về một chuỗi chứa mọi thứ xuất hiện sau dấu gạch chéo cuối cùng trong đối số
{"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
83. Tên thư mục và tên cơ sở của một đường dẫn được phác thảo trong.

Hình 8-4. Tên cơ sở theo sau dấu gạch chéo cuối cùng trong một đường dẫn và giống với tên tệp. Tên thư mục là mọi thứ trước dấu gạch chéo cuối cùng

Ví dụ: nhập thông tin sau vào trình bao tương tác

>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
8

Nếu bạn cần tên thư mục và tên cơ sở của đường dẫn cùng nhau, bạn chỉ cần gọi

>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
37 để nhận giá trị bộ dữ liệu với hai chuỗi này, như vậy

>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
3

Lưu ý rằng bạn có thể tạo cùng một bộ bằng cách gọi

>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
38 và
>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
39 và đặt các giá trị trả về của chúng vào một bộ

>>> import os
>>> os.path.join['usr', 'bin', 'spam']
'usr\\bin\\spam'
2

Nhưng

>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
37 là một lối tắt hay nếu bạn cần cả hai giá trị

Ngoài ra, lưu ý rằng

>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
37 không không lấy đường dẫn tệp và trả về danh sách các chuỗi của từng thư mục. Đối với điều đó, hãy sử dụng phương thức chuỗi
>>> import os
>>> os.path.join['usr', 'bin', 'spam']
'usr\\bin\\spam'
22 và phân tách trên chuỗi trong
>>> import os
>>> os.path.join['usr', 'bin', 'spam']
'usr\\bin\\spam'
23. Nhớ lại từ trước rằng biến
>>> import os
>>> os.path.join['usr', 'bin', 'spam']
'usr\\bin\\spam'
23 được đặt thành dấu gạch chéo phân tách thư mục chính xác cho máy tính đang chạy chương trình.

Ví dụ: nhập thông tin sau vào trình bao tương tác

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
00

Trên các hệ thống OS X và Linux, sẽ có một chuỗi trống ở đầu danh sách được trả về

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
01

Phương thức chuỗi

>>> import os
>>> os.path.join['usr', 'bin', 'spam']
'usr\\bin\\spam'
22 sẽ hoạt động để trả về danh sách từng phần của đường dẫn. Nó sẽ hoạt động trên mọi hệ điều hành nếu bạn vượt qua nó
>>> import os
>>> os.path.join['usr', 'bin', 'spam']
'usr\\bin\\spam'
26

Tìm kích thước tệp và nội dung thư mục

Khi bạn có cách xử lý đường dẫn tệp, bạn có thể bắt đầu thu thập thông tin về các tệp và thư mục cụ thể. Mô-đun

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
73 cung cấp các hàm để tìm kích thước của tệp theo byte cũng như các tệp và thư mục bên trong một thư mục nhất định

  • Gọi

    >>> import os
    >>> os.path.join['usr', 'bin', 'spam']
    'usr\\bin\\spam'
    28
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    83
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    84 sẽ trả về kích thước tính bằng byte của tệp trong
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    83
    argument.

  • Gọi

    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    002
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    83
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    84 sẽ trả về danh sách các chuỗi tên tệp cho mỗi tệp trong
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    83< . [Lưu ý rằng chức năng này nằm trong mô-đun
    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    76, không phải
    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    73. ]
    argument. [Note that this function is in the
    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    76 module, not
    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    73.]

Đây là những gì tôi nhận được khi thử các chức năng này trong trình bao tương tác

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
02

Như bạn thấy, calc. exe trên máy tính của tôi có kích thước 776.192 byte và tôi có rất nhiều tệp trong C. \Windows\system32 . Nếu tôi muốn tìm tổng kích thước của tất cả các tệp trong thư mục này, tôi có thể sử dụng đồng thời

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
008 và
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
009.

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
03

Khi tôi lặp qua từng tên tệp trong C. \Windows\System32 , biến

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
010 được tăng theo kích thước của mỗi tệp. Lưu ý khi tôi gọi
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
008, tôi sử dụng
>>> import os
>>> os.makedirs['C:\\delicious\\walnut\\waffles']
1 để nối tên thư mục với tên tệp hiện tại. Số nguyên mà
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
008 trả về được thêm vào giá trị của
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
010. Sau khi lặp qua tất cả các tệp, tôi in
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
010 để xem tổng kích thước của C. Thư mục \Windows\System32 .

Kiểm tra tính hợp lệ của đường dẫn

Nhiều hàm Python sẽ gặp lỗi nếu bạn cung cấp cho chúng một đường dẫn không tồn tại. Mô-đun

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
73 cung cấp các chức năng để kiểm tra xem đường dẫn đã cho có tồn tại hay không và liệu đó có phải là tệp hoặc thư mục hay không

  • Gọi

    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    017
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    83
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    84 sẽ trả về
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    88 nếu tệp hoặc thư mục được tham chiếu trong đối số tồn tại và sẽ trả về
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    89 nếu nó không tồn tại.

  • Gọi

    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    022
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    83
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    84 sẽ trả về
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    88 nếu đối số đường dẫn tồn tại và là một tệp và sẽ trả về
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    89 nếu không.

  • Gọi

    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    027
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    83
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    84 sẽ trả về
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    88 nếu đối số đường dẫn tồn tại và là một thư mục và sẽ trả về
    {"prtg": {"text": "Z:\\workswift False Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    89 nếu không.

Đây là những gì tôi nhận được khi thử các chức năng này trong trình bao tương tác

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
04

Bạn có thể xác định xem hiện tại có ổ đĩa DVD hoặc flash được gắn vào máy tính hay không bằng cách kiểm tra bằng chức năng

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
032. Chẳng hạn, nếu tôi muốn kiểm tra ổ đĩa flash có ổ đĩa có tên D. \ trên máy tính Windows của mình, tôi có thể làm điều đó bằng cách sau.

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
05

Ối. Có vẻ như tôi đã quên cắm ổ đĩa flash của mình

Quá trình đọc/ghi tệp

Sau khi bạn cảm thấy thoải mái khi làm việc với các thư mục và đường dẫn tương đối, bạn sẽ có thể chỉ định vị trí của tệp để đọc và ghi. Các chức năng được đề cập trong một số phần tiếp theo sẽ áp dụng cho các tệp văn bản gốc. Tệp văn bản gốc chỉ chứa các ký tự văn bản cơ bản và không bao gồm thông tin về phông chữ, kích thước hoặc màu sắc. Các tệp văn bản có . txt hoặc tệp tập lệnh Python với . phần mở rộng py là các ví dụ về tệp văn bản gốc. Chúng có thể được mở bằng Windows Notepad hoặc ứng dụng TextEdit của OS X. Các chương trình của bạn có thể dễ dàng đọc nội dung của các tệp văn bản gốc và coi chúng như một giá trị chuỗi thông thường.

Tệp nhị phân là tất cả các loại tệp khác, chẳng hạn như tài liệu soạn thảo văn bản, PDF, hình ảnh, bảng tính và chương trình thực thi. Nếu bạn mở một tệp nhị phân trong Notepad hoặc TextEdit, nó sẽ giống như bị xáo trộn vô nghĩa, giống như trong.

Hình 8-5. Chương trình Windows

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
033 được mở trong Notepad

Vì mỗi loại tệp nhị phân khác nhau phải được xử lý theo cách riêng của nó, cuốn sách này sẽ không đi sâu vào việc đọc và ghi các tệp nhị phân thô một cách trực tiếp. May mắn thay, nhiều mô-đun giúp làm việc với các tệp nhị phân dễ dàng hơn—bạn sẽ khám phá một trong số chúng, mô-đun

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
034, ở phần sau của chương này

Có ba bước để đọc hoặc ghi tệp trong Python

  1. Gọi hàm

    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    035 để trả về một đối tượng
    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    036

  2. Gọi phương thức

    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    037 hoặc
    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    038 trên đối tượng
    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    036

  3. Đóng tệp bằng cách gọi phương thức

    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    040 trên đối tượng
    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    036

Mở tệp bằng hàm open[]

Để mở một tệp bằng hàm

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
035, bạn chuyển cho nó một đường dẫn chuỗi cho biết tệp bạn muốn mở; . Hàm
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
035 trả về một đối tượng
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
036

Hãy thử bằng cách tạo một tệp văn bản có tên xin chào. txt bằng Notepad hoặc TextEdit. Nhập ______1045 làm nội dung của tệp văn bản này và lưu nó vào thư mục nhà người dùng của bạn. Sau đó, nếu bạn đang sử dụng Windows, hãy nhập thông tin sau vào trình bao tương tác.

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
06

Nếu bạn đang sử dụng OS X, hãy nhập thông tin sau vào trình bao tương tác

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
07

Đảm bảo thay thế ____1046 bằng tên người dùng máy tính của bạn. Ví dụ: tên người dùng của tôi là asweigart , vì vậy tôi sẽ nhập

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
047 trên Windows.

Cả hai lệnh này sẽ mở tệp ở chế độ “đọc văn bản gốc” hoặc viết tắt là chế độ đọc . Khi tệp được mở ở chế độ đọc, Python cho phép bạn chỉ đọc dữ liệu từ tệp; . Chế độ đọc là chế độ mặc định cho các tệp bạn mở bằng Python. Nhưng nếu bạn không muốn dựa vào giá trị mặc định của Python, bạn có thể chỉ định rõ ràng chế độ bằng cách chuyển giá trị chuỗi

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
048 làm đối số thứ hai cho
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
035. Vì vậy,
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
050 và
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
051 làm điều tương tự.

Cuộc gọi đến

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
035 trả về một đối tượng
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
036. Một đối tượng
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
036 đại diện cho một tệp trên máy tính của bạn; . Trong ví dụ trước, bạn đã lưu trữ đối tượng
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
036 trong biến
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
056. Bây giờ, bất cứ khi nào bạn muốn đọc hoặc ghi vào tệp, bạn có thể làm như vậy bằng cách gọi các phương thức trên đối tượng
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
036 trong
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
056

Đọc nội dung của tập tin

Bây giờ bạn đã có đối tượng

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
036, bạn có thể bắt đầu đọc từ đối tượng đó. Nếu bạn muốn đọc toàn bộ nội dung của tệp dưới dạng giá trị chuỗi, hãy sử dụng phương thức
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
037 của đối tượng
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
036. Hãy tiếp tục với xin chào. txt đối tượng
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
036 mà bạn đã lưu trữ trong
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
056. Nhập thông tin sau vào vỏ tương tác.

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
08

Nếu bạn coi nội dung của một tệp là một giá trị chuỗi lớn duy nhất, thì phương thức

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
037 sẽ trả về chuỗi được lưu trữ trong tệp

Ngoài ra, bạn có thể sử dụng phương pháp

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
065 để lấy danh sách các giá trị chuỗi từ tệp, một chuỗi cho mỗi dòng văn bản. Ví dụ: tạo một tệp có tên sonnet29.txt in the same directory as hello.txt and write the following text in it:

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
09

Đảm bảo phân tách bốn dòng bằng ngắt dòng. Sau đó nhập thông tin sau vào Shell tương tác

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
30

Lưu ý rằng mỗi giá trị chuỗi kết thúc bằng một ký tự xuống dòng,

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
066, ngoại trừ dòng cuối cùng của tệp. Một danh sách các chuỗi thường dễ làm việc hơn một giá trị chuỗi lớn duy nhất

Ghi vào tập tin

Python cho phép bạn ghi nội dung vào tệp theo cách tương tự như cách hàm

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
067 “ghi” chuỗi lên màn hình. Tuy nhiên, bạn không thể ghi vào tệp mà bạn đã mở ở chế độ đọc. Thay vào đó, bạn cần mở nó ở chế độ “ghi văn bản gốc” hoặc chế độ “chắp thêm văn bản gốc” hoặc chế độ ghiappend mode for short.

Chế độ ghi sẽ ghi đè lên tệp hiện có và bắt đầu lại từ đầu, giống như khi bạn ghi đè lên một giá trị của biến bằng một giá trị mới. Vượt qua

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
068 làm đối số thứ hai cho
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
035 để mở tệp ở chế độ ghi. Mặt khác, chế độ nối thêm sẽ nối văn bản vào cuối tệp hiện có. Bạn có thể coi điều này giống như việc thêm vào một danh sách trong một biến, thay vì ghi đè hoàn toàn biến đó. Vượt qua
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
070 làm đối số thứ hai cho
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
035 để mở tệp ở chế độ chắp thêm

Nếu tên tệp được chuyển đến

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
035 không tồn tại, cả chế độ ghi và nối sẽ tạo một tệp mới, trống. Sau khi đọc hoặc ghi tệp, hãy gọi phương thức
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
040 trước khi mở lại tệp

Hãy đặt những khái niệm này lại với nhau. Nhập thông tin sau vào vỏ tương tác

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
31

Đầu tiên, chúng ta mở thịt xông khói. txt ở chế độ ghi. Vì không có thịt xông khói. txt chưa, Python tạo một. Gọi

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
038 trên tệp đã mở và chuyển đối số chuỗi
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
038
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
076 ghi chuỗi vào tệp và trả về số ký tự đã viết, bao gồm cả dòng mới. Sau đó, chúng tôi đóng tập tin.

Để thêm văn bản vào nội dung hiện có của tệp thay vì thay thế chuỗi vừa viết, chúng tôi mở tệp ở chế độ chắp thêm. Chúng tôi viết

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
077 vào tệp và đóng nó. Cuối cùng, để in nội dung tệp ra màn hình, chúng tôi mở tệp ở chế độ đọc mặc định, gọi
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
037, lưu trữ đối tượng
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
036 kết quả trong
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
080, đóng tệp và in
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
080

Lưu ý rằng phương thức

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
038 không tự động thêm ký tự xuống dòng vào cuối chuỗi giống như hàm
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
067. Bạn sẽ phải tự thêm nhân vật này

Lưu các biến với Mô-đun giá

Bạn có thể lưu các biến trong chương trình Python của mình vào các tệp giá nhị phân bằng cách sử dụng mô-đun

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
034. Bằng cách này, chương trình của bạn có thể khôi phục dữ liệu cho các biến từ ổ cứng. Mô-đun
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
034 sẽ cho phép bạn thêm các tính năng Lưu và Mở vào chương trình của mình. Ví dụ: nếu bạn đã chạy một chương trình và nhập một số cài đặt cấu hình, bạn có thể lưu các cài đặt đó vào tệp giá sách và sau đó yêu cầu chương trình tải chúng vào lần chạy tiếp theo

Nhập thông tin sau vào vỏ tương tác

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
32

Để đọc và ghi dữ liệu bằng mô-đun

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
034, trước tiên bạn nhập
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
034. Gọi
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
088 và đặt cho nó một tên tệp, sau đó lưu trữ giá trị giá được trả về trong một biến. Bạn có thể thay đổi giá trị giá như thể nó là một từ điển. Khi bạn hoàn tất, hãy gọi
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
040 trên giá trị giá. Ở đây, giá trị hạn sử dụng của chúng tôi được lưu trữ trong
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
090. Chúng tôi tạo một danh sách
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
091 và viết
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
092 để lưu trữ danh sách trong
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
090 dưới dạng một giá trị được liên kết với khóa
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
094 [giống như trong từ điển]. Sau đó, chúng tôi gọi
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
040 trên
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
090

Sau khi chạy mã trước đó trên Windows, bạn sẽ thấy ba tệp mới trong thư mục làm việc hiện tại. dữ liệu của tôi. bak , dữ liệu của tôi. datdữ liệu của tôi. thư mục . Trên OS X, chỉ một mydata duy nhất. tệp db sẽ được tạo.

Các tệp nhị phân này chứa dữ liệu bạn đã lưu trữ trong giá sách của mình. Định dạng của các tệp nhị phân này không quan trọng; . Mô-đun này giúp bạn không phải lo lắng về cách lưu trữ dữ liệu của chương trình vào một tệp

Các chương trình của bạn có thể sử dụng mô-đun

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
034 để sau này mở lại và truy xuất dữ liệu từ các tệp giá sách này. Các giá trị giá không cần phải được mở ở chế độ đọc hoặc ghi—chúng có thể thực hiện cả hai sau khi được mở. Nhập thông tin sau vào vỏ tương tác

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
33

Ở đây, chúng tôi mở các tệp kệ để kiểm tra xem dữ liệu của chúng tôi có được lưu trữ chính xác không. Nhập

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
099 trả về cùng một danh sách mà chúng tôi đã lưu trữ trước đó, vì vậy chúng tôi biết rằng danh sách được lưu trữ chính xác và chúng tôi gọi
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
040

Cũng giống như từ điển, các giá trị của giá có các phương thức

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
301 và
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
302 sẽ trả về các giá trị giống như danh sách của các khóa và giá trị trong giá. Vì các phương thức này trả về các giá trị dạng danh sách thay vì danh sách thực, bạn nên chuyển chúng tới hàm
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
303 để lấy chúng ở dạng danh sách. Nhập thông tin sau vào vỏ tương tác

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
34

Văn bản gốc rất hữu ích để tạo các tệp mà bạn sẽ đọc trong trình soạn thảo văn bản như Notepad hoặc TextEdit, nhưng nếu bạn muốn lưu dữ liệu từ các chương trình Python của mình, hãy sử dụng mô-đun

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
034

Lưu các biến với pprint. hàm pformat[]

Nhớ lại rằng hàm

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
305 sẽ “in đẹp” nội dung của danh sách hoặc từ điển ra màn hình, trong khi hàm
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
306 sẽ trả về cùng văn bản này dưới dạng chuỗi thay vì in nó. Chuỗi này không chỉ được định dạng để dễ đọc mà còn là mã Python đúng về mặt cú pháp. Giả sử bạn có một từ điển được lưu trữ trong một biến và bạn muốn lưu biến này và nội dung của nó để sử dụng trong tương lai. Sử dụng
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
306 sẽ cung cấp cho bạn một chuỗi mà bạn có thể ghi vào . tệp py . Tệp này sẽ là mô-đun rất riêng của bạn mà bạn có thể nhập bất cứ khi nào bạn muốn sử dụng biến được lưu trữ trong đó.

Ví dụ: nhập thông tin sau vào trình bao tương tác

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
35

Ở đây, chúng tôi nhập

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
308 để cho chúng tôi sử dụng
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
306. Chúng tôi có một danh sách các từ điển, được lưu trữ trong một biến
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
091. Để giữ danh sách trong
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
091 khả dụng ngay cả sau khi chúng tôi đóng trình bao, chúng tôi sử dụng
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
306 để trả về dưới dạng chuỗi. Sau khi chúng tôi có dữ liệu trong
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
091 dưới dạng chuỗi, thật dễ dàng để ghi chuỗi vào một tệp mà chúng tôi sẽ gọi là myCats. p .

Bản thân các mô-đun mà câu lệnh

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
314 nhập chỉ là các tập lệnh Python. Khi chuỗi từ
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
306 được lưu vào một . py , tệp này là một mô-đun có thể được nhập giống như bất kỳ tệp nào khác.

Và vì bản thân các tập lệnh Python chỉ là các tệp văn bản có . py , các chương trình Python của bạn thậm chí có thể tạo các chương trình Python khác. Sau đó, bạn có thể nhập các tệp này vào tập lệnh.

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
36

Lợi ích của việc tạo . py [trái ngược với việc lưu các biến với mô-đun

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
034] bởi vì nó là một tệp văn bản, nên bất kỳ ai cũng có thể đọc và sửa đổi nội dung của tệp bằng một trình soạn thảo văn bản đơn giản. Tuy nhiên, đối với hầu hết các ứng dụng, lưu dữ liệu bằng cách sử dụng mô-đun
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
034 là cách ưu tiên để lưu các biến vào một tệp. Chỉ các kiểu dữ liệu cơ bản như số nguyên, số float, chuỗi, danh sách và từ điển mới có thể được ghi vào một tệp dưới dạng văn bản đơn giản. Ví dụ: các đối tượng
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
036 không thể được mã hóa dưới dạng văn bản.

Dự định. Tạo tệp câu hỏi ngẫu nhiên

Giả sử bạn là giáo viên địa lý với 35 học sinh trong lớp và bạn muốn đưa ra một câu đố vui về thủ phủ các bang của Hoa Kỳ. Than ôi, lớp của bạn có một vài quả trứng xấu trong đó, và bạn không thể tin học sinh không gian lận. Bạn muốn sắp xếp ngẫu nhiên thứ tự các câu hỏi để mỗi câu hỏi là duy nhất, khiến không ai có thể lấy câu trả lời từ bất kỳ ai khác. Tất nhiên, làm điều này bằng tay sẽ là một công việc dài và nhàm chán. May mắn thay, bạn biết một số Python

Đây là những gì chương trình làm

  • Tạo 35 câu đố khác nhau

  • Tạo 50 câu hỏi trắc nghiệm cho mỗi bài kiểm tra, theo thứ tự ngẫu nhiên

  • Cung cấp câu trả lời đúng và ba câu trả lời sai ngẫu nhiên cho mỗi câu hỏi, theo thứ tự ngẫu nhiên

  • Viết các câu đố vào 35 tệp văn bản

  • Viết các phím trả lời cho 35 tệp văn bản

Điều này có nghĩa là mã sẽ cần phải làm như sau

  • Lưu trữ các tiểu bang và thủ đô của họ trong một từ điển

  • Gọi

    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    035,
    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    038 và
    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    040 để nhận các tệp văn bản chính của bài kiểm tra và câu trả lời

  • Sử dụng

    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    322 để sắp xếp ngẫu nhiên thứ tự các câu hỏi và tùy chọn trắc nghiệm

Bước 1. Lưu trữ dữ liệu bài kiểm tra trong từ điển

Bước đầu tiên là tạo tập lệnh khung và điền vào đó dữ liệu bài kiểm tra của bạn. Tạo một tệp có tên randomQuizGenerator. py và làm cho nó giống như sau.

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
37

Vì chương trình này sẽ sắp xếp ngẫu nhiên các câu hỏi và câu trả lời, nên bạn cần nhập mô-đun

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
323 ❶ để sử dụng các chức năng của nó. Biến
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
324 ❷ chứa một từ điển với các tiểu bang của Hoa Kỳ là khóa và chữ hoa của chúng là giá trị. Và vì bạn muốn tạo 35 câu đố, mã thực sự tạo ra các tệp chính của câu hỏi và câu trả lời [được đánh dấu bằng
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
325 nhận xét hiện tại] sẽ đi vào bên trong vòng lặp
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
326 lặp lại 35 lần ❸. [Số này có thể được thay đổi để tạo bất kỳ số lượng tệp bài kiểm tra nào. ]

Bước 2. Tạo tệp câu hỏi và xáo trộn thứ tự câu hỏi

Bây giờ là lúc để bắt đầu điền vào những

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
325 đó

Mã trong vòng lặp sẽ được lặp lại 35 lần—một lần cho mỗi bài kiểm tra—vì vậy bạn chỉ phải lo lắng về một bài kiểm tra tại một thời điểm trong vòng lặp. Trước tiên, bạn sẽ tạo tệp bài kiểm tra thực tế. Nó cần phải có một tên tệp duy nhất và cũng phải có một số loại tiêu đề tiêu chuẩn trong đó, với các vị trí để học sinh điền tên, ngày tháng và tiết học. Sau đó, bạn sẽ cần lấy một danh sách các trạng thái theo thứ tự ngẫu nhiên, danh sách này có thể được sử dụng sau này để tạo câu hỏi và câu trả lời cho bài kiểm tra

Thêm các dòng mã sau vào randomQuizGenerator. p .

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
38

Tên tệp của các câu đố sẽ là capitalsquiz , trong đó là một số duy nhất cho bài kiểm tra đến từ

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
328, bộ đếm của vòng lặp
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
326. Đáp án cho capitalsquiz sẽ được lưu trữ trong tệp văn bản có tên capitalsquiz_answers . Mỗi lần qua vòng lặp, trình giữ chỗ
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
330 trong
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
331 và
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
332 sẽ được thay thế bằng
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
333, do đó, câu đố và câu trả lời đầu tiên được tạo sẽ là capitalsquiz1. txtcapitalsquiz_answers1. txt . Các tệp này sẽ được tạo bằng lệnh gọi hàm
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
035 tại ❶ và ❷, với
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
068 làm đối số thứ hai để mở chúng ở chế độ ghi.

Câu lệnh

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
038 tại ❸ tạo tiêu đề bài kiểm tra để học sinh điền vào. Cuối cùng, một danh sách ngẫu nhiên các tiểu bang của Hoa Kỳ được tạo với sự trợ giúp của hàm
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
322 ❹, hàm này sắp xếp lại ngẫu nhiên các giá trị trong bất kỳ danh sách nào được chuyển đến nó

Bước 3. Tạo tùy chọn trả lời

Bây giờ bạn cần tạo các tùy chọn trả lời cho mỗi câu hỏi, đây sẽ là nhiều lựa chọn từ A đến D. Bạn sẽ cần tạo một vòng lặp

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
326 khác—vòng lặp này để tạo nội dung cho từng câu hỏi trong số 50 câu hỏi trong bài kiểm tra. Sau đó, sẽ có một vòng lặp
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
326 thứ ba được lồng vào bên trong để tạo các tùy chọn trắc nghiệm cho mỗi câu hỏi. Làm cho mã của bạn trông giống như sau

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
39

Câu trả lời đúng rất dễ nhận được—nó được lưu trữ dưới dạng một giá trị trong từ điển

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
324 ❶. Vòng lặp này sẽ lặp qua các tiểu bang trong danh sách
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
341 đã xáo trộn, từ
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
342 đến
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
343, tìm từng tiểu bang trong
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
324 và lưu trữ vốn tương ứng của tiểu bang đó trong
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
345

Danh sách các câu trả lời sai có thể phức tạp hơn. Bạn có thể lấy nó bằng cách sao chép tất cả các giá trị trong từ điển

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
324 ❷, xóa câu trả lời đúng ❸ và chọn ba giá trị ngẫu nhiên từ danh sách này ❹. Hàm
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
347 giúp dễ dàng thực hiện lựa chọn này. Đối số đầu tiên của nó là danh sách bạn muốn chọn từ đó; . Danh sách đầy đủ các phương án trả lời là sự kết hợp của ba câu trả lời sai này với câu trả lời đúng ❺. Cuối cùng, các câu trả lời cần được chọn ngẫu nhiên ❻ để câu trả lời đúng không phải lúc nào cũng là lựa chọn D.

Bước 4. Viết nội dung cho các tệp chính của câu hỏi và câu trả lời

Tất cả những gì còn lại là viết câu hỏi vào tệp bài kiểm tra và câu trả lời vào tệp câu trả lời. Làm cho mã của bạn trông giống như sau

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
40

Một vòng lặp

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
326 đi qua các số nguyên
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
349 đến
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
350 sẽ ghi các phương án trả lời vào danh sách
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
351 ❶. Biểu thức
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
352 tại ❷ coi chuỗi
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
353 là một mảng và sẽ đánh giá thành
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
354,
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
355,
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
356 và sau đó là
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
357 trên mỗi lần lặp tương ứng thông qua vòng lặp

Ở dòng cuối cùng ❸, biểu thức

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
358 sẽ tìm chỉ số nguyên của câu trả lời đúng trong các tùy chọn câu trả lời được sắp xếp ngẫu nhiên và
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
359 sẽ đánh giá chữ cái của câu trả lời đúng để ghi vào tệp khóa câu trả lời

Sau khi bạn chạy chương trình, đây là cách capitalsquiz1 của bạn. txt sẽ hiển thị, mặc dù tất nhiên các tùy chọn câu hỏi và câu trả lời của bạn có thể khác với những tùy chọn được hiển thị ở đây, tùy thuộc vào kết quả của cuộc gọi

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
322 của bạn.

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
41

Tương ứng capitalsquiz_answers1. txt tệp văn bản sẽ trông như thế này.

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
42

Dự định. bảng nhiều ngăn

Giả sử bạn có nhiệm vụ nhàm chán là điền vào nhiều biểu mẫu trong một trang web hoặc phần mềm có nhiều trường văn bản. Bảng nhớ tạm giúp bạn không phải gõ đi gõ lại cùng một văn bản. Nhưng mỗi lần chỉ có một thứ có thể có trên khay nhớ tạm. Nếu bạn có nhiều đoạn văn bản khác nhau cần sao chép và dán, bạn phải tiếp tục đánh dấu và sao chép lặp đi lặp lại một vài nội dung giống nhau

Bạn có thể viết chương trình Python để theo dõi nhiều đoạn văn bản. “Bảng nhiều ngăn” này sẽ được đặt tên là mcb. pyw [vì “mcb” dễ gõ hơn “multiclipboard”]. . pyw có nghĩa là Python sẽ không hiển thị cửa sổ Terminal khi nó chạy chương trình này. [Xem Phụ lục B để biết thêm chi tiết. ]

Chương trình sẽ lưu từng đoạn văn bản trong clipboard dưới một từ khóa. Ví dụ: khi bạn chạy

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
361, nội dung hiện tại của khay nhớ tạm sẽ được lưu với từ khóa spam . Văn bản này sau đó có thể được tải lại vào khay nhớ tạm bằng cách chạy
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
362. Và nếu người dùng quên những từ khóa họ có, họ có thể chạy
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
363 để sao chép danh sách tất cả các từ khóa vào khay nhớ tạm.

Đây là những gì chương trình làm

  • Đối số dòng lệnh cho từ khóa được chọn

  • Nếu đối số là

    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    364 thì nội dung khay nhớ tạm được lưu vào từ khóa

  • Nếu đối số là

    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    365, thì tất cả các từ khóa sẽ được sao chép vào khay nhớ tạm

  • Nếu không, văn bản cho từ khóa sẽ được sao chép vào khay nhớ tạm

Điều này có nghĩa là mã sẽ cần phải làm như sau

  • Đọc các đối số dòng lệnh từ

    {"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
    
    366

  • Đọc và ghi vào clipboard

  • Lưu và tải vào một tệp kệ

Nếu bạn sử dụng Windows, bạn có thể dễ dàng chạy tập lệnh này từ Run. cửa sổ bằng cách tạo một tệp bó có tên mcb. bat với nội dung sau.

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
43

Bước 1. Nhận xét và Thiết lập Kệ

Hãy bắt đầu bằng cách tạo một kịch bản khung với một số nhận xét và thiết lập cơ bản. Làm cho mã của bạn trông giống như sau

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
44

Thực tế phổ biến là đưa thông tin sử dụng chung vào phần nhận xét ở đầu tệp ❶. Nếu bạn quên cách chạy tập lệnh của mình, bạn luôn có thể xem các nhận xét này để được nhắc nhở. Sau đó, bạn nhập các mô-đun của mình ❷. Sao chép và dán sẽ yêu cầu mô-đun

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
367 và đọc các đối số dòng lệnh sẽ yêu cầu mô-đun
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
368. Mô-đun
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
034 cũng sẽ có ích. Bất cứ khi nào người dùng muốn lưu một đoạn văn bản mới vào khay nhớ tạm, bạn sẽ lưu nó vào tệp giá sách. Sau đó, khi người dùng muốn dán văn bản trở lại khay nhớ tạm của họ, bạn sẽ mở tệp kệ và tải lại vào chương trình của mình. Tệp kệ sẽ được đặt tên với tiền tố mcb ❸.

Bước 2. Lưu nội dung Clipboard bằng từ khóa

Chương trình thực hiện những việc khác nhau tùy thuộc vào việc người dùng muốn lưu văn bản vào từ khóa, tải văn bản vào khay nhớ tạm hay liệt kê tất cả các từ khóa hiện có. Hãy giải quyết trường hợp đầu tiên đó. Làm cho mã của bạn trông giống như sau

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
45

Nếu đối số dòng lệnh đầu tiên [sẽ luôn ở chỉ mục

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
370 của danh sách
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
366] là
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
372 ❶, thì đối số dòng lệnh thứ hai là từ khóa cho nội dung hiện tại của khay nhớ tạm. Từ khóa sẽ được sử dụng làm khóa cho
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
373 và giá trị sẽ là văn bản hiện có trên khay nhớ tạm ❷

Nếu chỉ có một đối số dòng lệnh, bạn sẽ cho rằng đó là

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
374 hoặc một từ khóa để tải nội dung vào khay nhớ tạm. Bạn sẽ triển khai mã đó sau. Hiện tại, chỉ cần đặt một bình luận
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
325 ở đó ❸

Bước 3. Liệt kê từ khóa và tải nội dung của từ khóa

Cuối cùng, hãy thực hiện hai trường hợp còn lại. Người dùng muốn tải văn bản trong khay nhớ tạm từ một từ khóa hoặc họ muốn có danh sách tất cả các từ khóa có sẵn. Làm cho mã của bạn trông giống như sau

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
46

Nếu chỉ có một đối số dòng lệnh, trước tiên hãy kiểm tra xem đó có phải là

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
374 không ❶. Nếu vậy, một chuỗi đại diện cho danh sách các phím giá sẽ được sao chép vào khay nhớ tạm ❷. Người dùng có thể dán danh sách này vào trình soạn thảo văn bản đang mở để đọc

Mặt khác, bạn có thể cho rằng đối số dòng lệnh là một từ khóa. Nếu từ khóa này tồn tại trong giá sách

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
373 dưới dạng khóa, bạn có thể tải giá trị vào khay nhớ tạm ❸

Và đó là nó. Khởi chạy chương trình này có các bước khác nhau tùy thuộc vào hệ điều hành mà máy tính của bạn sử dụng. Xem Phụ lục B để biết chi tiết về hệ điều hành của bạn

Nhớ lại chương trình khóa mật khẩu mà bạn đã tạo trong Chương 6 đã lưu trữ mật khẩu trong từ điển. Cập nhật mật khẩu yêu cầu thay đổi mã nguồn của chương trình. Điều này không lý tưởng vì người dùng bình thường không cảm thấy thoải mái khi thay đổi mã nguồn để cập nhật phần mềm của họ. Ngoài ra, mỗi khi bạn sửa đổi mã nguồn của một chương trình, bạn sẽ có nguy cơ vô tình đưa ra các lỗi mới. Bằng cách lưu trữ dữ liệu cho chương trình ở một nơi khác với mã, bạn có thể làm cho chương trình của mình dễ sử dụng hơn cho người khác và có khả năng chống lại lỗi cao hơn

Tóm lược

Các tệp được sắp xếp thành các thư mục [còn được gọi là thư mục] và một đường dẫn mô tả vị trí của tệp. Mọi chương trình đang chạy trên máy tính của bạn đều có một thư mục làm việc hiện tại, cho phép bạn chỉ định đường dẫn tệp liên quan đến vị trí hiện tại thay vì luôn nhập đường dẫn đầy đủ [hoặc tuyệt đối]. Mô-đun

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
73 có nhiều chức năng để thao tác với đường dẫn tệp

Các chương trình của bạn cũng có thể tương tác trực tiếp với nội dung của tệp văn bản. Hàm

{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
035 có thể mở các tệp này để đọc nội dung của chúng dưới dạng một chuỗi lớn [với phương thức
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
037] hoặc dưới dạng danh sách các chuỗi [với phương thức
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
065]. Hàm
{"prtg": {"text": "Z:\\workswift True Administrator", "result": [{"Channel": "Percentage", "Value": 87, "Unit": "Percent", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute", "LimitMaxError": "90", "LimitMode": 1, "LimitMinError": "10", "LimitErrorMsg": "Percentage too high"}, {"Channel": "Response Time", "Value": 4711, "Unit": "TimeResponse", "SpeedSize": "One", "VolumeSize": "One", "SpeedTime": "Second", "Mode": "Absolute"}]}}
035 có thể mở tệp ở chế độ ghi hoặc nối thêm để tạo tệp văn bản mới hoặc thêm vào tệp văn bản hiện có, tương ứng

Trong các chương trước, bạn đã sử dụng khay nhớ tạm như một cách để đưa một lượng lớn văn bản vào một chương trình, thay vì nhập tất cả vào. Giờ đây, bạn có thể yêu cầu các chương trình của mình đọc tệp trực tiếp từ ổ cứng, đây là một cải tiến lớn vì tệp ít biến động hơn nhiều so với khay nhớ tạm

Trong chương tiếp theo, bạn sẽ học cách tự xử lý các tệp, bằng cách sao chép, xóa, đổi tên, di chuyển, v.v.

Python có thể truy cập thư mục dùng chung không?

lập trình trăn . Bạn có thể muốn chia sẻ tệp giữa máy tính và máy tính bảng hoặc máy tính khác mà bạn đã kết nối trên cùng một wifi. Bạn có thể làm điều đó một cách dễ dàng với Python. Hãy xem làm thế nào. You can easily access to a shared folder among your devices with Python. You may want to share files among a pc and a tablet or another pc that you have connected on the same wifi. You can do it vey easily with Python. Let's see how.

Làm cách nào để tạo tệp Python mới?

Cách tạo tệp bằng Python. Trong Python, bạn sử dụng hàm open[] với một trong các tùy chọn sau – "x" hoặc "w" – để tạo tệp mới . "x" – Tạo. lệnh này sẽ tạo một tệp mới khi và chỉ khi không có tệp nào tồn tại với tên đó, nếu không nó sẽ trả về lỗi.

Chủ Đề