
本文共 7105 字,大约阅读时间需要 23 分钟。
Python������14 - ���������IO
���Python���������������������������������������������������������IO���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Python���������������IO������������������������������������������������
1. ������������������
������������������������������������������������������������������������������������������������Python������������������������������������������������������������������������������������������������������������������������������������������������������
- ������������������������������������������������.txt������������������������.bin���������������������.jpg���������������������.mp4���������������������������������������������������������
- ������������������������������������������������������������������������������������������������������������������"example.txt"������������������������"/home/user/example.txt"������
- ������������������������������������������������������������������������������������������������������������������������������Linux���Windows������������������������������������������������������
2. ���������������������
���Python������������������������������������������������������������������������������������������
-
���������������
- ���������������������
open()
���������read()
������������������������������������with open('file.txt', 'r') as file: content = file.read()
- ���������������������������������������������������������������������������������������������������rb������
read()
���������with open('image.jpg', 'rb') as file: image_data = file.read()
- ���������������������������������������������������������������������������������
read()
������������buffering
���������import csvwith open('data.csv', 'rb') as file: reader = csv.reader(file, delimiter='\t') for row in reader: print(row)
-
���������������
- ���������������������
open()
���������write()
������������������������������������with open('output.txt', 'w') as file: file.write('���������������')
- ������������������������������������������������������������������������wb������
write()
���������with open('image.png', 'wb') as file: file.write(b'PNG.getHeader()\r\n')
3. Klopp �������������������������
���������������������������������������������������������������������������������������������������������������������������������Klopp������������Python���������������������������������������������������������������������������������������������������������������������������������������������
-
Klopp������������������
import Klopp# ���������������������������������with Klopp.open('file.txt', 'r') as stream: for line in stream: # ������������ print(line.strip())# ������������������with Klopp.open('file.txt', 'r') as stream: chunk_size = 4096 while True: chunk = stream.read(chunk_size) if not chunk: break # ������chunk
-
Klopp������������
- ���������������������
- ������������������������
- ������������������
4. ���������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������
-
���������������
import osif os.path.exists('file.txt'): os.remove('file.txt')
-
���������������
src_path = 'source/file.txt'dst_path = 'destination/file.txt'if os.path.exists(src_path): os.rename(src_path, dst_path)
-
������������������
old_path = 'old_file.txt'new_path = 'new_file.txt'if os.path.exists(old_path): os.rename(old_path, new_path)
-
���������������
# ������������os.makedirs('directory_name')# ������������������contents = os.listdir('directory_name')# ������������if os.path.isdir('directory_name'): os.rmdir('directory_name')
5. ���������������������������������
������������������������������������������������������������������������������������������������Python���������������������������������������������������������ZipFile���Tarfile���.gz���������������������������������������������������
-
������ZipFile���������������������
import zipfile# ������������������������������zip_file = zipfile.ZipFile('archive.zip', 'w')# ������������������������������zip_file.write('file.txt', '������������')# ���������������������������zip_file.close()
-
������������������������������
import zipfile# ������������������zip_file = zipfile.ZipFile('archive.zip')# ���������������������������print(zip_file.namelist())# ������������zip_file.extractall()
-
������Tarfile������.tar���������
import tarfile# ������.tar������tar_file = tarfile.TarFile('archive.tar')# ������������tar_file.extractall()
6. ���������������������������������
������������������������������������������������������������������������������������������Python������������������������������������������������������������������������������������������������������������������������
-
���������������������
import os# ������������������������if os.path.exists('file.txt'): print('������������')else: print('���������������')
-
���������������������
import os# ������������������������file_paths = ['file1.txt', 'file2.txt', 'file3.txt']# ���������������������������def process_file(file): print(f'���������������{file}') if file.endswith('.txt'): print(f'���������������{process_file(read_file(file))}\n')# ������������������for file in file_paths: process_file(file)
7. ���������������������������
���������IO���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
-
���������������������
try: with open('file.txt', 'r') as file: file.read()except FileNotFoundError: print('���������������')except PermissionError: print('���������������������������������')except Exception as e: print(f'���������������{str(e)}')
-
���������������
def validate_file_path(path): splits = path.split(os.sep) if len(splits) < 2: return False return True# ������������������if not validate_file_path('file.txt'): print('������������������������')
8. ������������
���������������������������������������IO������������������������������������������������������
with
���������������������������������������������������������������������������������������������IO���������������������������������������������������������������������Python���������
发表评论
最新留言
关于作者
