title: Python - copies Bad indent
body: <p>need a bit of help here <br></p>
  <p>I have a python script that copies and edits over txt files <br>
  but I'm having an issue with the indent <br>
  and I am not sure how to repair it <br></p>
  <p>The indent I normally use is <code>indent=4</code> and for the most part it has never failed <br>
  Now I am working on smaller files and the files have different indents so when it copies over the file for editing it removed 2 rows <br></p>
  <p>the original file starts with 6 indents and increases and decreases as it continues <br></p>
  <p>Here is the original look <br></p>
  <pre><code>{
  ......&quot;unid&quot;: 100,            - indent 6
        &quot;awar&quot;: false,
        &quot;upst&quot;: [
  ........{                     - indent 8
  ..........&quot;lvls&quot;: [           - indent 10
  ............{                 - indent 12
  ..............&quot;ulpf&quot;: true,   - indent 14
                &quot;fsg&quot;: [
                  0,
                  0,
                  0,
                  0,
                  0
                ]
              }
            ]
          }
        ],
        &quot;nupl&quot;: {
          &quot;txt&quot;: &quot;&quot;,
          &quot;cpf&quot;: true
        },
        &quot;esup&quot;: false,
        &quot;elcl&quot;: 0,
        &quot;gacp&quot;: false,
        &quot;paid&quot;: 5214,
        &quot;lzsid&quot;: 24434,
        &quot;livid&quot;: 214867,
        &quot;isra&quot;: false
      }
  </code></pre>
  <p>Here is the Results <br></p>
  <pre><code>Top of Market
  {
  ....&quot;unid&quot;: 1,            - indent 4
      &quot;awar&quot;: false,
      &quot;upst&quot;: [
  ........{                     - indent 8
  ............&quot;lvls&quot;: [           - indent 12
  ................{
                      &quot;ulpf&quot;: true,   - indent 16
                      &quot;fsg&quot;: [
                          0,
                          0,
                          0,
                          0,
                          0
                      ]
                  }
              ]
          }
      ],
      &quot;nupl&quot;: {
          &quot;txt&quot;: &quot;Marks1&quot;,
          &quot;cpf&quot;: true
      },
      &quot;esup&quot;: false,
      &quot;elcl&quot;: 0,
      &quot;gacp&quot;: false,
      &quot;paid&quot;: 125,
      &quot;lzsid&quot;: 24434,
      &quot;livid&quot;: 214867,
      &quot;isra&quot;: false
  }, 
  Bottom of Market
  </code></pre>
  <p>So when I increase the indent to 6 it places all the codes in the right spots but now this section  also increase by 2</p>
  <p>this is the result with 6 <br></p>
  <pre><code>Top of Market
  {
        &quot;unid&quot;: 1,
        &quot;awar&quot;: false,
        &quot;upst&quot;: [
              {
                    &quot;lvls&quot;: [
                          {
                                &quot;ulpf&quot;: true,  --- as you can see here it's moved over a ton
                                &quot;fsg&quot;: [   moved over 24 indents
                                      0,
                                      0,
                                      0,
                                      0,
                                      0
                                ]
                          }
                    ]
              }
        ],
        &quot;nupl&quot;: {
              &quot;txt&quot;: &quot;Marks1&quot;,
              &quot;cpf&quot;: true
        },
        &quot;esup&quot;: false,
        &quot;elcl&quot;: 0,
        &quot;gacp&quot;: false,
        &quot;paid&quot;: 125,
        &quot;lzsid&quot;: 24434,
        &quot;livid&quot;: 214867,
        &quot;isra&quot;: false
  }, 
  Bottom of Market
  </code></pre>
  <p>what is the best way to fix this problem </p>
  <p>Here is the script <br></p>
  <pre><code>import shutil
  import glob
  import json
  import re
  import asyncio
  import sys
  from asyncore import loop
  
  unit_to_paid = {&quot;(1)&quot;: 125,
                  &quot;(2)&quot;: 124}
  
  async def update_file(file, arg):
      f = open(file, &quot;r&quot;)
      text = f.read()
  
  
      data = json.loads(text)
      num = re.search(r&quot;\(([^()]+)\)&quot;, file)
  
  
      data[&quot;unid&quot;] = int(num.group(1))
      data[&quot;paid&quot;] = unit_to_paid[num.group(0)]
      data[&quot;nupl&quot;][&quot;txt&quot;] = f'Marks{num.group(1)}'
  
      f.close()
  
      f = open(file, &quot;w&quot;)
      f.truncate(0)
      json.dump(data, f, indent=4)   ------ here is the indent
      f.write(&quot;, \nBottom of Market&quot;)
      f.close()
  
      f1 = open(file, &quot;r+&quot;)
      content = f1.read()
      f1.seek(0, 0)
      f1.write(&quot;Top of Market&quot; + '\n' + content)
      f1.close()
  
  
  async def get_update_files(arg):
      # Recursively grabs the files
      files = glob.glob('New folder 2/**/*.txt', recursive=True)
  
      # Creates a list of tasks to run concurrently
      tasks = []
      for file in files:
          tasks.append(asyncio.create_task(update_file(file, arg)))
  
      t = asyncio.gather(*tasks)
  
      return t
  
  
  def main():
      args = sys.argv[1:]
      # Deletes New folder 2 if it exists
      shutil.rmtree('New folder 2', ignore_errors=True)
  
      # Copies existing files into New folder 2
      shutil.copytree('New folder 1', 'New folder 2')
  
      # Will get each file and update them concurrently
      if len(args) == 0:
          asyncio.get_event_loop().run_until_complete(get_update_files(&quot;all&quot;))
      else:
          asyncio.get_event_loop().run_until_complete(get_update_files(args[0]))
  
  main()
  </code></pre>
  <p>I have tried a few things but nothing works I even removed this section so to edit the originals <br></p>
  <pre><code># Deletes New folder 2 if it exists
          shutil.rmtree('New folder 2', ignore_errors=True)
      
          # Copies existing files into New folder 2
          shutil.copytree('New folder 1', 'New folder 2')
  </code></pre>
  <p>and updated this <code>files = glob.glob('New folder 1/**/*.txt', recursive=True)</code> <br></p>
  <p>Updated result <br></p>
  <pre><code>Empty row
  Empty row
  Empty row
  Empty row
  Empty row
  Empty row ......................1000 indents................ Top Market  
  {
          ......&quot;unid&quot;: 1,
                &quot;awar&quot;: false,
                &quot;upst&quot;: [
          ........{
          ..........&quot;lvls&quot;: [
          ............{
          ..............&quot;ulpf&quot;: true,
                        &quot;fsg&quot;: [
                          0,
                          0,
                          0,
                          0,
                          0
                        ]
                      }
                    ]
                  }
                ],
                &quot;nupl&quot;: {
                  &quot;txt&quot;: &quot;&quot;,
                  &quot;cpf&quot;: true
                },
                &quot;esup&quot;: false,
                &quot;elcl&quot;: 0,
                &quot;gacp&quot;: false,
                &quot;paid&quot;: 5214,
                &quot;lzsid&quot;: 24434,
                &quot;livid&quot;: 214867,
                &quot;isra&quot;: false
           } &lt; this shifted over with 0 indents
  </code></pre>