-2

I would testing if a file has already been open before writing.

Here my code :

with open(file_five, 'w') as f:
    f.write(xml)

I would something as this code example :

if "file_five has already been open"
        with open(file_five, 'w') as f:
           f.write(xml)
else:
...
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
PseudoWithK
  • 321
  • 1
  • 19

1 Answers1

-2

There are two ways: 1-> For Excel specific

try:
    myfile = open("file_five.csv", "r+") # or "a+", whatever you need
except IOError:
    print "Could not open file! !"

with myfile:
    do_stuff()

2 -> For any file (Rename approach)

import os

try: 
    os.rename('file.xls', 'tempfile.xls')
    os.rename('tempfile.xls', 'file.xls')
except OSError:
    print('File is still open.')
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
ashishmishra
  • 363
  • 2
  • 14