2

Of course, I understand how to read and write text files in Python. However, I need to be able to read a file without Windows considering it "open" in another process. Here's the deal:

I have a Python script running on my Raspberry Pi that pulls a 3-digit number from a text document on my laptop over the network. I want to be able to change this text document whenever I wish and save it so the Python script updates almost immediately. Here's generally what I have right now.

while True:
    text = open("/mnt/win/text.txt","r")
    rgb = text.read()
    text.close()

    ## ... Do stuff with rgb

    time.sleep(.2)

This works out fine, because I essentially have a (slightly more than) .2 second loop in which the file "text.txt" is only open for a fairly small portion. However, whenever I modify "text.txt" through notepad, there is a chance that hitting save will give me an error saying the file is already open in another process. So that makes me wonder; is there a way around it? I have seen something similar in which the person used a PHP script to read the file and dumped that into their Python code, but I was kind of hoping to keep it all Python. Thanks!

EDIT: By the way, if it wasn't apparent, the folder that contains the text file on Windows was mounted to /mnt/win on Linux.

Asaaj
  • 272
  • 2
  • 3
  • 9
  • 1
    how about copying the current "version" of the textfile into a temporary space, process the temp file and then recalculate checksums to ensure the file hasn't changed? – inspectorG4dget Aug 29 '13 at 20:44
  • At any point in the copying process, would the file be "in use" by the script? Cause if not, that was a really simple fix. – Asaaj Aug 29 '13 at 20:48
  • I should have specified that by `copy`, I meant that you should use something like `os` or `shutil`. Take a look at [this](http://stackoverflow.com/a/12843464/198633) – inspectorG4dget Aug 29 '13 at 20:50
  • What do you want to happen if you're in the middle of that `text.read()` and Notepad writes to the file? (As you may have guessed, the whole point of Windows' default mandatory file locking is to make it so you don't have to think about and answer that question…) – abarnert Aug 29 '13 at 20:51
  • @inspectorG4dget: How are you going to recalculate checksums without opening the file and reading it in? – abarnert Aug 29 '13 at 20:51
  • @abarnert: D'Oh! I seem to have had a blond moment. Withdrawn – inspectorG4dget Aug 29 '13 at 20:53
  • I think you can use Linux's inotify facility to watch for file changes without having the file open, and only open it after the file has changed, but I am not sure if this will work over an SMB mount. Here is a python wrapper around inotify: http://pyinotify.sourceforge.net/ – mkimball Aug 29 '13 at 20:54

1 Answers1

0

Not sure why I totally didn't think of it, but I ended up just making a copy of the file that I called temp. It's not the cleanest approach, but it works perfectly.

I just put shutil.copyfile("/mnt/win/text.txt","/mnt/win/temp.txt") before the text = open(...) during the loop, so the original text file seems to be left mostly untouched by the program.

Such a simple solution I kind of feel ridiculous for asking

Asaaj
  • 272
  • 2
  • 3
  • 9
  • So what magic does `shutil.copyfile` employ that keeps *it* from asserting the file lock? Whatever it is, maybe you could do the same thing in your original program. – Robᵩ Aug 29 '13 at 21:05
  • I'm not sure. To be honest, it probably still locks the file. But in using it a number of times, I haven't run in to a problem. My guess is that it's such a short lock it doesn't matter. I would have to do more research though – Asaaj Aug 29 '13 at 21:09