Python3 .7 запись объекта в файл - PullRequest
0 голосов
/ 04 мая 2020

Я пытаюсь записать объект в новый файл, но все, что пишет, это пустая строка в файл. Вот мой код:

source = FileHandler.get_absolute_path('../source')
repo = FileHandler.get_absolute_path('../repo')
patches = FileHandler.get_absolute_path('../patches')

files = filecmp.dircmp('../source/', '../repo/')
write_path = '../patches/'


def open_file(filename):
    with open(filename) as f:
        lines = f.readlines()
        return lines


def write_to_new_file(filename, contents):
    with open(filename, 'w') as f:
        for line in contents:
            f.writelines(line)


def report_file_diff(dcmp):
    for name in dcmp.diff_files:
        print("DIFF file %s found in %s and %s" % (name,
                                                   dcmp.left, dcmp.right))
        fromfile = os.path.abspath(dcmp.left + name)
        tofile = os.path.abspath(dcmp.right + name)
        newfile = os.path.abspath(write_path + name)
        source = open_file(fromfile)
        repo = open_file(tofile)
        diff = difflib.unified_diff(source, repo, fromfile=fromfile, tofile=tofile, lineterm='\n')
        result = Colorize.color_diff(diff)
        print(''.join(result), end="")
        write_to_new_file(newfile, diff)
    for name in dcmp.left_only:
        print("NEW SOURCE file found: %s" % (fromfile))
    for name in dcmp.right_only:
        print("NEW REMOTE file found: %s" % (tofile))
    for sub_dcmp in dcmp.subdirs.values():
        print(sub_dcmp)


report_file_diff(files)

Результаты diff, которые должны быть записаны в файл:

--- /Users/justinboucher/PycharmProjects/splunk_app_deployment/source/itsi_docs.xml
+++ /Users/justinboucher/PycharmProjects/splunk_app_deployment/repo/itsi_docs.xml
@@ -1,4 +1,4 @@
-<form theme="light">
+<form theme="light" stylesheet="markdown_styles.css">
   <label>ITSI Documentation</label>
   <fieldset submitButton="true" autoRun="true">
     <input type="dropdown" token="service" searchWhenChanged="true">
@@ -59,7 +59,7 @@
     <panel depends="$doc_link$">
       <viz type="aplura_viz_markdown.markdown">
         <search>
-          <query>| makeresults</query>
+          <query>| savedsearch=$doc_link$</query>
           <earliest>-24h@h</earliest>
           <latest>now</latest>
           <sampleRatio>1</sampleRatio>
@@ -71,7 +71,7 @@
         <option name="aplura_viz_markdown.markdown.log">false</option>
         <option name="aplura_viz_markdown.markdown.path_name">docs</option>
         <option name="aplura_viz_markdown.markdown.search_field">markdown</option>
-        <option name="aplura_viz_markdown.markdown.sonic_screwdriver">local_file</option>
+        <option name="aplura_viz_markdown.markdown.sonic_screwdriver">inline_html</option>
         <option name="drilldown">none</option>
         <option name="refresh.display">progressbar</option>
         <option name="trellis.enabled">0</option>

Но вместо этого я не получаю ни одной строки, записанной в новый файл. Но это создание файла. Я не могу понять, почему строки не пишутся. Будем очень благодарны любой помощи. Заранее спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...