From 97a828bb8717a45af28890e54024ffbe4c5e9df6 Mon Sep 17 00:00:00 2001 From: mrcool7387 <146818600+mrcool7387@users.noreply.github.com> Date: Fri, 3 Jan 2025 21:56:37 +0100 Subject: [PATCH 1/2] Created snippet file-operations.md --- .../python/file-handling/file-operations.md | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 snippets/python/file-handling/file-operations.md diff --git a/snippets/python/file-handling/file-operations.md b/snippets/python/file-handling/file-operations.md new file mode 100644 index 00000000..e60b028c --- /dev/null +++ b/snippets/python/file-handling/file-operations.md @@ -0,0 +1,55 @@ +--- +title: File Operations Class +description: It's an class where you have some functions for file Opertation +author: mrcool7387 +tags: python,file operation,class +--- + + +```py +class FileOperations: + def create_file(self, file_path: str): + with open(file_path, 'w') as f: + pass + + def delete_file(self, file_path: str): + if os.path.exists(file_path): + os.remove(file_path) + + def write_file(self, file_path: str, context: str): + with open(file_path, 'w') as f: + f.write(context) + + def move_file(self, src: str, dest: str): + shutil.move(src, dest) + + def copy_file(self, src: str, dest: str): + shutil.copy(src, dest) + + def get_file_size(self, file_path: str): + return os.path.getsize(file_path) if os.path.isfile(file_path) else 0 + + def get_file_extension(self, file_path: str): + return os.path.splitext(file_path)[1] + + def zip_files(self, files: list[str], zip_name: str): + 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): + with zipfile.ZipFile(zip_name, 'r') as zf: + zf.extractall(extract_to) + +# Usage +fo = FileOperations() + +fo.create_file('./notes/note1.txt') +fo.create_file('./note2.txt') + +fo.write_file('./notes/note1.txt', 'Very Important Notes!') + +fo.zip_files(['./notes/note1.txt', './note2.txt'], 'notes.zip') + +fo.delete_file('./notes/note1.txt') +``` 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 2/2] 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)