Debugging
Image Processing
import shutil import os IMAGE_FILE_EXTENSIONS = [".png", ".jpeg", ".jpg", ".svg", ".tiff", ".webp",".ppm"] def move_image_files(source: str, target: str): for img in [img for img in os.listdir(source) if os.path.splitext(img)[-1] in IMAGE_FILE_EXTENSIONS]: new_path = os.path.join(target, img) shutil.move(os.path.join(source, img), new_path)
- import shutil
- import os
class MoveFiles:def __init__(self):self.images = [img for img in os.listdir('folderA') if img.endswith(('.png', '.jpg', '.jpeg'))]- IMAGE_FILE_EXTENSIONS = [".png", ".jpeg", ".jpg", ".svg", ".tiff", ".webp",".ppm"]
def move_image_files(self):for img in self.images:new_path = os.path.join('folderB', img)shutil.move(os.path.join('folderA', img), new_path)- def move_image_files(source: str, target: str):
- for img in [img for img in os.listdir(source) if os.path.splitext(img)[-1] in IMAGE_FILE_EXTENSIONS]:
- new_path = os.path.join(target, img)
- shutil.move(os.path.join(source, img), new_path)
import codewars_test as test from solution import move_image_files, delete_test_files import os @test.describe("Example") def test_group(): @test.it("test case 1: File quantity") def test_case(): move_image_files("folderA","folderB") t1 = len(os.listdir('folderA')) t2 = len(os.listdir('folderB')) test.assert_equals(t1, 5) test.assert_equals(t2, 7) @test.it("test case 2: Check file type") def test_case(): move_image_files("folderA","folderB") t1 = len([img for img in os.listdir('folderA') if img.endswith(('.png', '.jpg', '.jpeg'))]) t2 = len([img for img in os.listdir('folderB') if img.endswith(('.png', '.jpg', '.jpeg'))]) t3 = len([img for img in os.listdir('folderA') if img.endswith('.txt')]) t4 = len([img for img in os.listdir('folderB') if img.endswith('.txt')]) test.assert_equals(t1, 0) test.assert_equals(t2, 7) test.assert_equals(t3, 5) test.assert_equals(t4, 0) delete_test_files()
- import codewars_test as test
from solution import MoveFiles, delete_test_files- from solution import move_image_files, delete_test_files
- import os
- @test.describe("Example")
- def test_group():
- @test.it("test case 1: File quantity")
- def test_case():
MoveFiles().move_image_files()- move_image_files("folderA","folderB")
- t1 = len(os.listdir('folderA'))
- t2 = len(os.listdir('folderB'))
- test.assert_equals(t1, 5)
- test.assert_equals(t2, 7)
- @test.it("test case 2: Check file type")
- def test_case():
MoveFiles().move_image_files()- move_image_files("folderA","folderB")
- t1 = len([img for img in os.listdir('folderA') if img.endswith(('.png', '.jpg', '.jpeg'))])
- t2 = len([img for img in os.listdir('folderB') if img.endswith(('.png', '.jpg', '.jpeg'))])
- t3 = len([img for img in os.listdir('folderA') if img.endswith('.txt')])
- t4 = len([img for img in os.listdir('folderB') if img.endswith('.txt')])
- test.assert_equals(t1, 0)
- test.assert_equals(t2, 7)
- test.assert_equals(t3, 5)
- test.assert_equals(t4, 0)
- delete_test_files()