-1

I have a text file on sd card which I am reading through python. I want to copy contents of this file and save in another sd card, and as I make changes to original file it should reflect run time on the copied version in real time

I went through Copying from one text file to another using Python but this is static implementation(copied file does not change runtime with changes in original file)

My code:

import os

with open("/path/to/file.txt", 'r') as f:
 print (f.read())
 #f.flush()


file = open("/path/to/another/file.txt", 'w')
while True:
    file.write( f)
    file.flush()
   # file.close()
cs95
  • 379,657
  • 97
  • 704
  • 746
Ajinkya
  • 1,797
  • 3
  • 24
  • 54
  • What software is changing the original? Do you control the source code? The obvious way is to alter that program to operate on both. – Prune Oct 26 '17 at 17:07
  • If you don't have access to the source code of the program that is modifying the file you need to have a separate process that watches the original file and copy it to the destination file if changed. Check some solutions here: https://stackoverflow.com/q/182197/3885927 – user3885927 Oct 26 '17 at 17:10
  • I am using python to generate random text in orignal one. My aim is to use another python script(as shown above in the question) to see if I can get the random text saved on sd card as it being generated – Ajinkya Oct 26 '17 at 17:11

1 Answers1

1

If you're trying to track any changes in the original file, from any source, then you have a problem with system security. By definition of the file resource, the OS sees them as distinct entities.

The usual way to handle this is with periodic back-ups. If you require real-time response, then leave a small program running that will detect writes to the original file (ala Tripwire security) and make the changes on demand.

In general, this is not a Python solution; rather it's something to code at the OS level.

Prune
  • 76,765
  • 14
  • 60
  • 81