Python-字符串末尾的空格从哪里来?

时间:2019-01-02 11:36:51

标签: python string list set

TL; DR-什么会在存储在集合中的字符串的末尾生成空白?

问题描述: 想象一下编写脚本来爬过目录并对过滤器文件中提到的目录的特定更改执行操作。 由于某些体系结构限制,您选择让脚本映射dir结构并将其逐行存储在文本文件中。示例:

./Test
./Test/foo
./Test/foo/bar

然后,当您再次运行脚本时,它将把 n 运行中生成的新文本文件与 n-1 运行中的旧文本文件进行比较。如果有任何更改,它将通过将更改与包含感兴趣目录的过滤器文本文件进行比较来对它们进行操作。

但是,运行脚本时,它将失败。调试后,事实证明,在“更改”集中存储的字符串的末尾之前添加了一个空格,如下所示:

changed_path = {str}'.\\Test\\foo\\bar \n' # Detected Path change
watched_path = {str}'.\\Test\\foo\\bar\n' # Path as detailed in filter file

知道这是什么原因吗? 问题似乎在“检查器”功能中 完整代码:

# Script

################
# Relevant Imports
###############
import os

################
# Parameter definitions:
###############
PATH = "."  # default is the root folder holding the script
FILTERS = "./Filters/"  # Default is a folder named "Filters" in root folder
SUFFIX = ".txt"
DIFF = []


################
# Function definitions
###############
def return_as_list(filter_file):
    """Creates a list of strings out of a text file, line by line"""
    with open(FILTERS+filter_file, 'r') as filter:
        results = filter.readlines()
        return results


def checker(filter_as_list, changes_as_set):
    """Compares the filter list to the changes set, and returns
        the difference if there is any"""
    #changes_set = set(filter_as_list).intersection(changes_as_set)
    changes_list = []
    for watched_path in filter_as_list:
        for changed_path in changes_as_set:
            if watched_path == changed_path:
                changes_list.append(watched_path)

    if not changes_list:  # If the list is empty
        print('changes_list is empty!')
        pass  # Skips loop since there is no match between filter and Changelog
    else:
        return (changes_list)


###############
# Generate log of changes made to the directory structure:
###############
# Check if log exists, if so designate it as old for comparison later
if os.path.isfile('log_current.txt'):
    if not os.path.isfile('log_old.txt'):
        os.rename('log_current.txt', 'log_old.txt')


# Fill log_current with current directory structure
for root, dirs, files in os.walk(PATH, topdown=True):
    for name in dirs:
        os.system('echo ' + os.path.join(root, name) + ' >> log_current.txt')


# If two logs exist, create a log of all differences between the two
if (os.path.isfile('log_current.txt') and os.path.isfile('log_old.txt')):
    with open('log_current.txt', 'r') as current:
        with open('log_old.txt', 'r') as old:
            DIFF = set(current).symmetric_difference(old)

    # Create a text file containing all changes
    with open('log_changes.txt', 'w+') as changes:
        for line in DIFF:
            changes.write(line)


###############
# Script start
###############
# Check if the changes set is empty
if not DIFF:
    print('Set is empty, no changes')
    if os.path.isfile('log_old.txt'):
        os.remove('log_old.txt')
    if os.path.isfile('log_changes.txt'):
        os.remove('log_changes.txt')

else:
    # Cycle through the filters and compare them to the changes log
    for filename in os.listdir(FILTERS):
        if filename.endswith(SUFFIX):
            # Retrieve email address and send file to be processed
            filter_file_as_list = return_as_list(filename)
            email_address = filter_file_as_list.pop(0)

            # Get a list of changes that are mentioned in the filter file
            list_of_changed_paths = checker(filter_file_as_list, DIFF)

            # If there was no match
            if not list_of_changed_paths:
                pass
            else:
                paths_for_email = ("\n".join(list_of_changed_paths))
                # Create text file for Email script
                with open('message.txt', 'w+') as msg:
                    msg.write("The following paths have changed:\n " + paths_for_email)
                print(paths_for_email)
                #os.system("./outlook-sendmail.exe " + email_address + ' "Changes" ' + ' "message.txt"')


                # Clean up
            if os.path.isfile('message.txt'):
                os.remove("message.txt")
            if os.path.isfile('log_old.txt'):
                os.remove('log_old.txt')
            if os.path.isfile('log_changes.txt'):
                os.remove('log_changes.txt')

0 个答案:

没有答案