From a06cc1af80f138589e2464e4d87a7df7000a8b88 Mon Sep 17 00:00:00 2001 From: mrcool7387 <146818600+mrcool7387@users.noreply.github.com> Date: Fri, 3 Jan 2025 22:05:39 +0100 Subject: [PATCH] Updated snipped file-operations.md for spelling, comments and missing imports --- snippets/python/file-handling/file-operations.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/snippets/python/file-handling/file-operations.md b/snippets/python/file-handling/file-operations.md index e60b028c..91d58102 100644 --- a/snippets/python/file-handling/file-operations.md +++ b/snippets/python/file-handling/file-operations.md @@ -1,43 +1,56 @@ --- title: File Operations Class -description: It's an class where you have some functions for file Opertation +description: It's an class where you have some functions for file Operation author: mrcool7387 tags: python,file operation,class --- ```py +import zipfile +import os +import shutil + class FileOperations: def create_file(self, file_path: str): + """Creates an empty File""" with open(file_path, 'w') as f: pass def delete_file(self, file_path: str): + """Deletes a File""" if os.path.exists(file_path): os.remove(file_path) def write_file(self, file_path: str, context: str): + """Writes some text to a file. '\n' is used fo a new Line""" with open(file_path, 'w') as f: f.write(context) def move_file(self, src: str, dest: str): + """Moves a File form its Source to it's destination""" shutil.move(src, dest) def copy_file(self, src: str, dest: str): + """Copies a File form its Source to it's destination""" shutil.copy(src, dest) def get_file_size(self, file_path: str): + """Returns the File size of the selected File""" return os.path.getsize(file_path) if os.path.isfile(file_path) else 0 def get_file_extension(self, file_path: str): + """Returns the Type/Extension of the selected File""" return os.path.splitext(file_path)[1] def zip_files(self, files: list[str], zip_name: str): + """Compresses some selected Files, don't forget the '.zip' at the end of the ZIP-File-Name""" with zipfile.ZipFile(zip_name, 'w') as zf: for file in files: zf.write(file) def extract_zip(self, zip_name: str, extract_to: str): + """Extracts from a selected ZIP File to a Destination""" with zipfile.ZipFile(zip_name, 'r') as zf: zf.extractall(extract_to)