Search code examples
pythondownloadgoogle-colaboratory

After modifying a text file using RegEx, how do I download the newly modified file?


I just modified a text file using RegEx expressions in Google Collab. Specifically, I stripped a text file of three digit values. Now, I want to save the output and download the newly modified text file. How would I do this in Python?

For reference, here is my code.

filename = "gpt2historianregex.txt"

def main():
  fh = open("gpt2historianregex.txt")
  for line in fh:
    print( re. sub('\d{3}','', text), end='')


if __name__ == "__main__": main()

Solution

  • To download the output, the file needs to be written first.

    filename = "gpt2historianregex.txt"
    
    def main():
      fh = open("gpt2historianregex.txt")
      with open('out_file.txt', 'w') as out:
          for line in fh:
            out.write(re.sub('\d{3}','', line), end=''))
      fh.close()
    
    
    
    
    if __name__ == "__main__": main()
    

    The file then should appear on the left bar in Google Colab.