0

I'm writing an ableton python script. This one writes the string to the file:

class LaunchControl(ControlSurface):    

    def __init__(self, c_instance):
        super(LaunchControl, self).__init__(c_instance)

        f = open("d:\members2.txt", "w")
        f.write('START ok\n\n')
        f.flush()

But this one does not and I don't see any error in the log. The only difference is the last line:

class LaunchControl(ControlSurface):    

    def __init__(self, c_instance):
        super(LaunchControl, self).__init__(c_instance)

        f = open("d:\members2.txt", "w")
        f.write('START ok\n\n')
        f.flush()
        self.f = f

I want to use f in other functions of LaunchControl class

Ivan Zakharov
  • 461
  • 5
  • 6
  • but if I do f.close() it works! – Ivan Zakharov Sep 29 '18 at 08:26
  • Comments are for others to ask for clarification etc. If you want to add additional informatino to yout question, always add it by hitting the [edit](https://stackoverflow.com/posts/52566469/edit) button. In case you are adding something because someone asked for it, add it to the question and then leave a comment including @+username to ensure that that person gets notified – FlyingTeller Sep 29 '18 at 08:39
  • What do you plan to use `f` for? Is it just going to be log output ? – FlyingTeller Sep 29 '18 at 08:48
  • thanks everyone. I am new to python so stupid questions happen. Just was wondering why flush doesn't work – Ivan Zakharov Sep 30 '18 at 20:44

2 Answers2

1

Leaving your file opened is a bad habit. What happens if other applications need to read or write to the same file? Since you've opened it in write mode, it is blocked and no other application can access it until it is closed (released).

If you want to access your file from multiple functions or scripts, save it's filename:

self.filename = "d:\members2.txt"

and when you need, you open (and then close) it.


As a suggestion, don't use f = open(...). Use the safe keyword with.

with open("d:\members2.txt", 'w') as f:
    f.write('...')

After exiting from the with scope, the resource (in this case a file stream) is automatically closed and released. It is safety closed even in the case of exceptions thrown. The python documentation says (emphasis added):

with statement allows the execution of initialization and finalization code around a block of code

Moreover, you don't need to explicitly flush the file. After exiting the with block, the file is automatically closed and flushed

Neb
  • 2,270
  • 1
  • 12
  • 22
-1

Following the docs (on os.fsync):

If you’re starting with a Python file object f, first do f.flush(), and then do os.fsync(f.fileno()), to ensure that all internal buffers associated with f are written to disk.

So you should do

class LaunchControl(ControlSurface):    

    def __init__(self, c_instance):
        super(LaunchControl, self).__init__(c_instance)

        f = open("d:\members2.txt", "w")
        f.write('START ok\n\n')
        f.flush()
        os.fsync(f.fileno())
        self.f = f
FlyingTeller
  • 17,638
  • 3
  • 38
  • 53