Kim Kardashian
Kim Kardashian

Reputation: 91

Python does not recognize soft link change

import os
import sys
from time import sleep

soft = 'link.lnk'
fd_soft = open(soft, 'a');
i = 0;
while 1:
    try:
        line = 'the count is %d' %(i)
        print line
        fd_soft.write(line);
        i += 1;
        sleep(1);
        fd_soft.flush()
    except KeyboardInterrupt:
        print "interrupt ctrl c"
        fd_soft.close()
        sys.exit(0)

link.lnk is a soft link to a old.txt file. This script opens the soft link and writes a number to it every second.
During runtime i change link.lnk to point to a new file with

ln -sf new.txt link.lnk

because i want to write to new.txt, but the process still keeps writing to old file.

Upvotes: 0

Views: 93

Answers (1)

Joe Young
Joe Young

Reputation: 5895

You're still using the old file handle that you opened before you changed the symlink. If you want new writes to be reflected in the new symlink, you'll need to re-open the file handle before each write and close it after each write.

Upvotes: 1

Related Questions