windows - Close a file opened with the filename (and not the process name) -
i working python 3.6
, on pycharm
, windows
, on function determine if file opened (by user, outside of script) able close before overwrite it. know if opened, try rename , in case of failure, means file opened (with talk csv file toto.csv open in excel example). part okay me. but, after 2 hours of research on google, didn't find how close file opened properly. did process psutil.process_iter()
in part of code executable, here seems more complicated. wish find solution close file opened name toto.csv.
here sample of code:
import os name = "toto.csv" try: os.rename(name,name) # if success, file closed except: # here want close file if it's opened # open, write, , close file on python environment myfile = open(name, 'w', newline='\n') # write data myfile.close()
anybody has clue me? thank in advance.
Comments
Post a Comment