Python chia tệp theo dòng

Giống như hầu hết trẻ em, tôi dành nhiều thời gian trên Internet. Theo như tôi có thể nói, đó là điều cần làm trong những ngày này. Trong thế hệ mới nhất này, những người đam mê máy tính và các bậc thầy dường như được tôn trọng giống như những ngôi sao nhạc rock từng dành cho tôi. Khi những đứa trẻ biến vào phòng của chúng, rất có thể chúng đang hack máy tính chứ không phải thành thạo các đoạn riff guitar. Nó có lẽ lành mạnh hơn một số trò giải trí trong tuổi trẻ sai lầm của tôi, nhưng đó là chủ đề cho một loại sách khác

Nhưng nếu bạn có con ở độ tuổi thanh thiếu niên và máy tính, hoặc biết ai đó làm như vậy, bạn có thể biết rằng theo dõi những gì những đứa trẻ đó làm trên Web là một ý tưởng không tồi. Nhập từ có bốn chữ cái yêu thích của bạn vào hầu hết mọi công cụ tìm kiếm trên web và bạn sẽ hiểu mối quan tâm -- đó là thứ tốt hơn nhiều so với những gì tôi có thể có được trong sự nghiệp tuổi teen của mình. Để giải quyết vấn đề, chỉ một số máy trong nhà tôi có nguồn cấp dữ liệu Internet

Giờ đây, khi đang sử dụng một trong những máy này, các con tôi tải xuống rất nhiều trò chơi. Tuy nhiên, để tránh lây nhiễm vi-rút từ các trò chơi trên phạm vi công cộng vào Máy tính Rất quan trọng của chúng tôi, các con tôi thường phải tải xuống trò chơi trên máy tính có nguồn cấp dữ liệu Internet và chuyển chúng sang máy tính của chúng để cài đặt. Vấn đề là các tệp trò chơi không nhỏ;

Nếu tất cả các máy trong nhà tôi chạy Linux, đây sẽ là một sự cố. Có các chương trình dòng lệnh tiêu chuẩn trên Unix để cắt tệp thành nhiều phần đủ nhỏ để vừa với đĩa mềm [tách] và các chương trình khác để ghép các phần lại với nhau để tạo lại tệp gốc [cat]. Tuy nhiên, vì chúng tôi có tất cả các loại máy móc khác nhau trong nhà nên chúng tôi cần một giải pháp di động hơn

Vì tất cả các máy tính trong nhà tôi đều chạy Python, nên một tập lệnh Python di động đơn giản đã đến giải cứu. Chương trình Python trong Ví dụ 4-1 phân phối nội dung của một tệp duy nhất trong một tập hợp các tệp bộ phận và lưu trữ các tệp bộ phận đó trong một thư mục

Ví dụ 4-1. PP2E\System\Filetools\split. py

#!/usr/bin/python
#########################################################
# split a file into a set of portions; join.py puts them
# back together; this is a customizable version of the 
# standard unix split command-line utility; because it
# is written in Python, it also works on Windows and can
# be easily tweaked; because it exports a function, it 
# can also be imported and reused in other applications;
#########################################################

import sys, os
kilobytes = 1024
megabytes = kilobytes * 1000
chunksize = int[1.4 * megabytes]                   # default: roughly a floppy

def split[fromfile, todir, chunksize=chunksize]: 
    if not os.path.exists[todir]:                  # caller handles errors
        os.mkdir[todir]                            # make dir, read/write parts
    else:
        for fname in os.listdir[todir]:            # delete any existing files
            os.remove[os.path.join[todir, fname]] 
    partnum = 0
    input = open[fromfile, 'rb']                   # use binary mode on Windows
    while 1:                                       # eof=empty string from read
        chunk = input.read[chunksize]              # get next part ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004

Mỗi trong số bốn tệp phần được tạo này đại diện cho một đoạn nhị phân của tệp

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
6, đủ nhỏ để vừa với đĩa mềm. Trên thực tế, nếu bạn thêm kích thước của các tệp bộ phận được tạo bởi lệnh ls, bạn sẽ có 5.028.339 byte -- chính xác bằng kích thước của tệp gốc. Trước khi chúng tôi xem cách đặt lại các tệp này lại với nhau, hãy khám phá một số điểm tốt hơn của tập lệnh bộ chia

Tập lệnh này được thiết kế để nhập các tham số của nó ở chế độ tương tác hoặc dòng lệnh; . Trong chế độ dòng lệnh, bạn liệt kê tệp sẽ được chia và thư mục đầu ra trên dòng lệnh và có thể tùy ý ghi đè kích thước tệp phần mặc định bằng đối số dòng lệnh thứ ba

Trong chế độ tương tác, tập lệnh yêu cầu tên tệp và thư mục đầu ra tại cửa sổ bảng điều khiển với

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
7 và tạm dừng để nhấn phím ở cuối trước khi thoát. Chế độ này rất hay khi tệp chương trình được khởi động bằng cách nhấp vào biểu tượng của nó -- trên Windows, các tham số được nhập vào hộp DOS bật lên không tự động biến mất. Tập lệnh cũng hiển thị đường dẫn tuyệt đối của các tham số của nó [bằng cách chạy chúng qua
C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
8] vì chúng có thể không rõ ràng trong chế độ tương tác. Chúng tôi sẽ xem các ví dụ về các chế độ phân chia khác đang hoạt động trong giây lát

Mã này cẩn thận để mở cả tệp đầu vào và tệp đầu ra ở chế độ nhị phân [

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
9,
 fileobj  = open[partname, 'wb']
 fileobj.write[chunk]
 fileobj.close[  ]
0], vì nó cần xử lý một cách hợp lý những thứ như tệp thực thi và tệp âm thanh, không chỉ văn bản. Trong Chương 2, chúng ta đã biết rằng trên Windows, các tệp ở chế độ văn bản sẽ tự động ánh xạ các chuỗi cuối dòng của
 fileobj  = open[partname, 'wb']
 fileobj.write[chunk]
 fileobj.close[  ]
1 thành
 fileobj  = open[partname, 'wb']
 fileobj.write[chunk]
 fileobj.close[  ]
2 trên đầu vào và ánh xạ
 fileobj  = open[partname, 'wb']
 fileobj.write[chunk]
 fileobj.close[  ]
2 thành
 fileobj  = open[partname, 'wb']
 fileobj.write[chunk]
 fileobj.close[  ]
1 trên đầu ra. Đối với dữ liệu nhị phân thực sự, chúng tôi thực sự không muốn bất kỳ ký tự
 fileobj  = open[partname, 'wb']
 fileobj.write[chunk]
 fileobj.close[  ]
5 nào trong dữ liệu biến mất khi đọc và chúng tôi không muốn thêm bất kỳ ký tự
 fileobj  = open[partname, 'wb']
 fileobj.write[chunk]
 fileobj.close[  ]
5 thừa nào vào đầu ra. Các tệp chế độ nhị phân chặn ánh xạ
 fileobj  = open[partname, 'wb']
 fileobj.write[chunk]
 fileobj.close[  ]
5 này khi tập lệnh được chạy trên Windows và do đó tránh hỏng dữ liệu

Tập lệnh này cũng không thể đóng các tệp của nó theo cách thủ công. Ví dụ

 fileobj  = open[partname, 'wb']
 fileobj.write[chunk]
 fileobj.close[  ]

Như chúng ta đã thấy trong Chương 2, ba dòng này thường có thể được thay thế bằng một dòng duy nhất

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
5

Biểu mẫu ngắn hơn này dựa trên thực tế là việc triển khai Python hiện tại sẽ tự động đóng các tệp cho bạn khi các đối tượng tệp được lấy lại [i. e. , khi chúng được thu gom rác, vì không còn tham chiếu nào đến đối tượng tệp]. Trong dòng này, đối tượng tệp sẽ được lấy lại ngay lập tức, vì kết quả

 fileobj  = open[partname, 'wb']
 fileobj.write[chunk]
 fileobj.close[  ]
8 là tạm thời trong một biểu thức và không bao giờ được tham chiếu bởi một tên tồn tại lâu hơn. Tệp
 fileobj  = open[partname, 'wb']
 fileobj.write[chunk]
 fileobj.close[  ]
9 tương tự được lấy lại khi hàm
C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
50 thoát

Tuy nhiên, khi tôi đang viết chương này, có một số khả năng là hành vi đóng tự động này có thể biến mất trong tương lai. [] Hơn nữa, việc triển khai Python dựa trên Java JPython không lấy lại các đối tượng không được ước tính ngay lập tức như Python tiêu chuẩn. Nếu bạn quan tâm đến cổng Java [hoặc một tương lai có thể xảy ra], thì tập lệnh của bạn có thể tạo ra nhiều tệp trong một khoảng thời gian ngắn và tập lệnh của bạn có thể chạy trên một máy có giới hạn về số lượng tệp đang mở trên mỗi chương trình, sau đó . Các lệnh gọi

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
51 trong tập lệnh này chưa bao giờ cần thiết cho mục đích của tôi, nhưng vì chức năng
C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
50 trong mô-đun này được dự định là một công cụ có mục đích chung nên nó phù hợp với các tình huống xấu nhất như vậy

Quay lại việc di chuyển các tập tin lớn xung quanh nhà. Sau khi tải xuống một tệp chương trình trò chơi lớn, các con tôi thường chạy tập lệnh bộ chia trước đó bằng cách nhấp vào tên của nó trong Windows Explorer và nhập tên tệp. Sau khi phân tách, họ chỉ cần sao chép từng tệp phần vào đĩa mềm của chính nó, đưa đĩa mềm lên tầng trên và tạo lại thư mục đầu ra đã phân tách trên máy tính mục tiêu của họ bằng cách sao chép tệp từ đĩa mềm. Cuối cùng, tập lệnh trong Ví dụ 4-2 được nhấp hoặc chạy để ghép các phần lại với nhau

Ví dụ 4-2. PP2E\System\Filetools\tham gia. py

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
1

Sau khi chạy tập lệnh

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
53, họ vẫn có thể cần chạy thứ gì đó như
C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
54,
C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
55 hoặc
C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
56 để giải nén tệp lưu trữ, trừ khi tệp đó được vận chuyển dưới dạng tệp thực thi;[] nhưng ít nhất họ cũng tiến gần hơn đến việc thấy Starship Enterprise bắt đầu hoạt động . Đây là quá trình nối đang diễn ra trên Windows, kết hợp các tệp đã chia mà chúng tôi đã tạo lúc trước

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
6

Tập lệnh nối chỉ cần sử dụng

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
57 để thu thập tất cả các tệp phần trong một thư mục được tạo bằng cách tách và sắp xếp danh sách tên tệp để đặt các phần lại với nhau theo đúng thứ tự. Chúng tôi lấy lại một bản sao chính xác từng byte của tệp gốc [được chứng minh bằng lệnh DOS fc ở trên; sử dụng cmp trên Unix]

Tất nhiên, một số quy trình này vẫn là thủ công [tôi vẫn chưa tìm ra cách viết kịch bản cho đoạn “đi bộ bằng đĩa mềm lên lầu” một chút], nhưng các kịch bản

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
50 và
C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
53 giúp việc di chuyển các tệp lớn trở nên nhanh chóng và đơn giản. Bởi vì tập lệnh này cũng là mã Python di động, nó chạy trên bất kỳ nền tảng nào mà chúng tôi quan tâm để di chuyển các tệp đã phân tách sang. Chẳng hạn, con tôi thường tải xuống cả trò chơi Windows và Linux;

Đọc theo khối hoặc tệp

Trước khi chúng ta tiếp tục, có một vài chi tiết đáng được nhấn mạnh trong mã của tập lệnh tham gia. Trước hết, hãy lưu ý rằng tập lệnh này xử lý các tệp ở chế độ nhị phân, nhưng cũng đọc từng tệp thành phần trong các khối 1K byte mỗi khối. Trên thực tế, cài đặt

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
10 ở đây [kích thước của mỗi khối được đọc từ tệp phần đầu vào] không liên quan đến
C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
11 trong
C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
12 [tổng kích thước của từng tệp phần đầu ra]. Như chúng ta đã học trong Chương 2, tập lệnh này thay vào đó có thể đọc từng tệp phần cùng một lúc

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
3

Nhược điểm của sơ đồ này là nó thực sự tải tất cả tệp vào bộ nhớ cùng một lúc. Ví dụ: đọc 1. 4M phần vào bộ nhớ cùng một lúc với đối tượng tệp phương pháp

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
13 tạo ra 1. 4M chuỗi trong bộ nhớ để giữ các byte của tệp. Vì
C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
50 cho phép người dùng chỉ định kích thước khối thậm chí còn lớn hơn, tập lệnh
C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
53 lập kế hoạch cho điều tồi tệ nhất và đọc theo các khối có kích thước giới hạn. Để hoàn toàn mạnh mẽ, tập lệnh
C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
50 cũng có thể đọc dữ liệu đầu vào của nó theo các phần nhỏ hơn, nhưng điều này không trở thành mối lo ngại trong thực tế

Nếu bạn nghiên cứu kỹ mã của tập lệnh này, bạn cũng có thể nhận thấy rằng sơ đồ nối mà nó sử dụng hoàn toàn dựa vào thứ tự sắp xếp của tên tệp trong thư mục bộ phận. Bởi vì nó chỉ đơn giản gọi phương thức list

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
17 trên danh sách tên tệp được trả về bởi
C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
57, nên nó hoàn toàn yêu cầu tên tệp có cùng độ dài và định dạng khi được tạo bằng cách tách. Bộ tách sử dụng ký hiệu không đệm trong biểu thức định dạng chuỗi [
C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
19] để đảm bảo rằng tất cả các tên tệp đều có cùng một số chữ số ở cuối [bốn], giống như danh sách này

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
1

Khi được sắp xếp, các ký tự số 0 ở đầu với số lượng nhỏ đảm bảo rằng các tệp bộ phận được sắp xếp để nối chính xác. Không có số 0 đứng đầu,

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
53 sẽ thất bại bất cứ khi nào có nhiều hơn chín phần tệp, vì chữ số đầu tiên sẽ chiếm ưu thế

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
3

Vì phương thức list

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
17 chấp nhận hàm so sánh làm đối số, nên về nguyên tắc, chúng ta có thể loại bỏ các chữ số trong tên tệp và sắp xếp theo số

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
5

Nhưng điều đó vẫn ngụ ý rằng tất cả các tên tệp phải bắt đầu bằng chuỗi con có cùng độ dài, vì vậy điều này không hoàn toàn loại bỏ sự phụ thuộc đặt tên tệp giữa các tập lệnh

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
50 và
C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
53. Tuy nhiên, vì các tập lệnh này được thiết kế thành hai bước của cùng một quy trình, nên một số phụ thuộc giữa chúng có vẻ hợp lý

Hãy chạy thêm một số thử nghiệm với các tiện ích hệ thống Python này để minh họa các chế độ sử dụng khác. Khi chạy mà không có đối số dòng lệnh đầy đủ, cả

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
50 và
C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
53 đều đủ thông minh để nhập tham số của chúng một cách tương tác. Ở đây họ đang cắt và dán lại tệp trình tự cài đặt Python trên Windows, với các tham số được nhập trong cửa sổ bảng điều khiển DOS

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
0

Khi các tệp chương trình này được nhấp đúp vào GUI của trình khám phá tệp, chúng sẽ hoạt động theo cùng một cách [thường không có đối số dòng lệnh khi khởi chạy theo cách này]. Trong chế độ này, hiển thị đường dẫn tuyệt đối giúp làm rõ vị trí thực sự của tệp. Hãy nhớ rằng, thư mục làm việc hiện tại là thư mục chính của tập lệnh khi được nhấp như thế này, vì vậy tên

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
66 thực sự ánh xạ tới một thư mục mã nguồn;

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
1

Tuy nhiên, vì các tập lệnh này đóng gói logic cốt lõi của chúng trong các hàm, nên việc sử dụng lại mã của chúng cũng dễ dàng bằng cách nhập và gọi từ một thành phần Python khác

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
2

Một từ về hiệu suất. Tất cả các bài kiểm tra

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
50 và
C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
53 được hiển thị cho đến nay đều xử lý tệp 5M, nhưng chỉ mất tối đa một giây thời gian đồng hồ treo tường thực để hoàn thành trên máy tính xách tay Windows 98 300 và 650 MHz của tôi -- rất nhanh cho bất kỳ mục đích sử dụng nào của tôi . [Chúng thậm chí còn chạy nhanh hơn sau khi Windows đã lưu trữ thông tin về các tệp liên quan. ] Cả hai tập lệnh cũng chạy nhanh như vậy đối với các kích thước tệp phần hợp lý khác;

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
3

Quá trình tách có thể mất nhiều thời gian hơn để hoàn tất, nhưng chỉ khi kích thước của tệp bộ phận được đặt đủ nhỏ để tạo ra hàng nghìn tệp bộ phận -- quá trình tách thành 1006 phần hoạt động nhưng chạy chậm hơn [trên máy tính của tôi, quá trình tách và nối này mất khoảng năm và hai giây,

C:\temp>echo %X%             
                  shorthand shell variable
C:\PP2ndEd\examples\PP2E

C:\temp>ls -l py152.exe
-rwxrwxrwa   1 0        0        5028339 Apr 16  1999 py152.exe

C:\temp>python %X%\System\Filetools\split.py -help
Use: split.py [file-to-split target-dir [chunksize]]

C:\temp>python %X%\System\Filetools\split.py py152.exe pysplit
Splitting C:\temp\py152.exe to C:\temp\pysplit by 1433600
Split finished: 4 parts are in C:\temp\pysplit

C:\temp>ls -l pysplit
total 9821
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0001
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0002
-rwxrwxrwa   1 0        0        1433600 Sep 12 06:03 part0003
-rwxrwxrwa   1 0        0         727539 Sep 12 06:03 part0004
4

Cuối cùng, bộ chia cũng đủ thông minh để tạo thư mục đầu ra nếu nó chưa tồn tại hoặc xóa mọi tệp cũ ở đó nếu nó tồn tại. Bởi vì trình nối kết hợp bất kỳ tệp nào tồn tại trong thư mục đầu ra, đây là một điểm nhấn công thái học tuyệt vời -- nếu thư mục đầu ra không bị xóa trước mỗi lần phân tách, bạn sẽ rất dễ quên rằng các tệp của lần chạy trước đó vẫn còn đó. Cho rằng con tôi đang chạy những tập lệnh này, chúng cần phải tha thứ nhất có thể;

Chủ Đề