在文件夹中查找包含文件名字符串的文件,然后删除这些文件中的字符串

时间:2020-03-30 17:02:52

标签: python-3.x

我希望脚本排除模板文件,排除文件名中没有10个字符串的任何文件,然后查找文件名中带有+的文件,然后在每个文件中删除+

无法弄清楚如何过滤文件名中包含+的文件

import os

directory_path=os.path.dirname(os.path.abspath(__file__))
template_file_name='? exchange symbol buyorsell positionSide longorshort positionOperator value= closeorder +-'

template_arr = template_file_name.split()
template_file_content = open(directory_path + "/" + template_file_name, "r").read()

for root, dirs, files in os.walk(directory_path):
    file_info = []
    for file in files:
        destination_filename = os.path.join(file)

#excludes template file

        if(destination_filename!=template_file_name):
            destination_filename_arr = destination_filename.split()
            if(len(destination_filename_arr) != 10):

#skips files that don't have 10 strings in filename

                print("Skipping this file: " + destination_filename)
                continue;

#need help filtering files that contain + in filename

                if(destination_filename_arr contains "+"):
                    continue;

                    #open files and replace strings

                    with open(os.path.join(directory_path, file), 'r+') as destination_filename_content:
                        for x in range(10):
                            destination_filename_content = destination_filename_content.replace("+", " ")
                        destination_file = open(directory_path + "/" + destination_filename,"w")
                        destination_file.write(destination_filename_content)

0 个答案:

没有答案