Python类中的可选参数

拉夫里·普拉内斯

这是我的python代码,用于执行删除,移动和重命名多个文件之类的操作。该代码对所有类型的文件都适用。假设我想添加一个可选参数,例如删除扩展名为“ .pdf”的文件,如何添加该选项?好像它不是强制性的,但是如果您通过它,则所有pdf文件都将被删除。

我只想添加一个可选参数,该参数在传递时仅对那些文件执行操作

import os


class FileOperation:
    def __init__(self, file_loc):
        self.file_loc = file_loc

    def file_location(self):
        print("Source folder contents: ")
        print()
        for i in os.listdir(self.file_loc):
            print(i)
        print()

    def rename_bulk_files(self):
        c = 0
        path = self.file_loc
        for i in os.listdir(self.file_loc):
            dot_index = i.index('.')
            src = os.path.join(path, i)
            dst = os.path.join(path, i[0:dot_index] + "_" + str(c) + i[dot_index:])
            os.rename(src, dst)
            c += 1
        return "Renaming of files has finished"

    def delete_bulk_files(self):
        path = self.file_loc
        for i in os.listdir(self.file_loc):
            file_path = os.path.join(path, i)
            os.remove(file_path)
        print("All files have been deleted")

    def move_bulk_files(self):
        path = self.file_loc
        destination_folder = input("Enter the destination folder / folders separated by a comma: ")
        for i in os.listdir(self.file_loc):
            src_path = os.path.join(path, i)
            des_path = os.path.join(destination_folder, i)
            os.replace(src_path, des_path)
        print("All files have been moved to the destination folder")


source_folder = input("Enter source folder/ folders locations separated by a comma: ")


my_file_operations = FileOperation(source_folder)
my_file_operations.delete_bulk_files()
普罗斯特
files_extension = input(“Enter files extension (defaults to all files if not provided): “)

然后在delete_bulk_files


file_path = os.path.join(path, i)
if not files_extension or file_path.endswith(files_extension):
  os.remove(file_path)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

python中的可选位置参数

来自分类Dev

类初始化中的可选参数

来自分类Dev

Powershell - 类方法中的可选参数

来自分类Dev

TypeScript中的类构造函数中的可选参数

来自分类Dev

PHP 中带有 $this 的类方法中的可选参数

来自分类Dev

在python中传递可选的dataframe参数

来自分类Dev

如何在 Python 中创建“可选”参数?

来自分类Dev

在 Python 中实现 JavaScript 的可选参数样式

来自分类Dev

Python:将可选参数装饰器实现为类

来自分类Dev

实体类中的scala spray json可选id参数

来自分类Dev

抽象类类型签名中的可选参数

来自分类Dev

派生抽象类覆盖中的可选参数

来自分类Dev

Typescript初始化类中的可选参数

来自分类Dev

派生抽象类覆盖中的可选参数

来自分类Dev

实体类中的scala spray json可选id参数

来自分类Dev

Dart 确保类中的可选 List 参数不为空

来自分类Dev

UML类图的可选参数

来自分类Dev

Python可选参数对

来自分类Dev

completeHandler中的可选参数

来自分类Dev

JavaScript中的可选参数

来自分类Dev

MongoDB中的可选参数

来自分类Dev

Argparse中的可选参数

来自分类Dev

如何在 django python 中的 url 中传递可选参数

来自分类Dev

适用于Notepad ++的Python脚本中的可选参数

来自分类Dev

将函数传递给 Python 中的可选参数

来自分类Dev

将可选函数(和可选参数)传递给 Python 中的另一个函数?

来自分类Dev

Python-函数的可选参数

来自分类Dev

SQL查询中的可选参数

来自分类Dev

处理QueryDSL中的可选参数