Skip to content

Created snippet file-operations.md #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions snippets/python/file-handling/file-operations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
title: File Operations Class
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)

# 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')
```
Loading