在Python中,切割文件可以通过多种方法实现,具体取决于你想要切割的文件类型和大小。以下是一些常见的方法:
1. 使用`seek`和`read`方法:
```python
filename = 'large_file.dat'
file_size = 10 * 1024 * 1024 10MB
with open(filename, 'rb') as f:
index = 0
while True:
f.seek(index * file_size)
data = f.read(file_size)
if not data:
break
with open(f'{filename}_{index}', 'wb') as f1:
f1.write(data)
index += 1
2. 使用`split`方法:
```python
filename = 'data_file.txt'
with open(filename, 'r') as file:
lines = file.readlines()
for i in range(0, len(lines), 10):
with open(f'{filename}_{i//10}.txt', 'w') as f:
f.writelines(lines[i:i+10])
3. 使用`shutil`模块:
```python
import shutil
source_file_path = 'bigfile.csv'
target_file_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'splits')
if not os.path.exists(target_file_dir):
os.makedirs(target_file_dir)
with open(source_file_path, 'r', encoding='utf-8-sig') as source_file:
for index, source_file_line in enumerate(source_file):
target_file_index = index % 10
with open(os.path.join(target_file_dir, f'split_{target_file_index}.csv'), 'a', encoding='utf-8-sig') as target_file:
target_file.write(source_file_line)
4. 针对PDF文件的切割:
```python
import sys
from PyPDF2 import PdfReader, PdfWriter
def split_pdf(input_path, output_path, start_page, end_page):
with open(input_path, 'rb') as input_file:
pdf = PdfReader(input_file)
total_pages = len(pdf.pages)
if start_page > total_pages:
start_page = total_pages
if end_page > total_pages:
end_page = total_pages
output_pdf = PdfWriter()
for page_num in range(start_page, end_page + 1):
output_pdf.add_page(pdf.pages[page_num])
with open(output_path, 'wb') as output_file:
output_pdf.write(output_file)
5. 均匀洒水方式进行切割:
```python
import os
source_file_path = 'bigfile.csv'
target_file_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'splits')
if not os.path.exists(target_file_dir):
os.makedirs(target_file_dir)
target_file_count = 10
target_file_list = [open(os.path.join(target_file_dir, f'split_{x}.csv'), 'a', encoding='utf-8-sig') for x in range(target_file_count)]
with open(source_file_path, 'r', encoding='utf-8-sig') as source_file:
for index, source_file_line in enumerate(source_file):
target_file_index = index % target_file_count
target_file_list[target_file_index].write(source_file_line)
以上代码示例展示了如何使用Python切割不同类型和大小的文件。请根据你的具体需求选择合适的方法。