I have a text file name s1.txt and want to check if its open in python 3
with open("s1.txt","r") as f:
x = f.read()
I did saw the similar question on SO but its for python 2.0 Any help on the topic would be great
I have a text file name s1.txt and want to check if its open in python 3
with open("s1.txt","r") as f:
x = f.read()
I did saw the similar question on SO but its for python 2.0 Any help on the topic would be great
f.closed should do the trick
For example
with open("myfile.txt","r") as f:
x = f.read()
is_file_open = not f.closed #returns true if file is open
print(is_file_open)
This will not detect if the file is open by other processes!
If you want to check that then
import os
def is_file_open(filename):
try:
os.rename(filename, filename)
return False
except:
return True
print(is_file_open("myfile.txt")) # return true if file is open
You can use closed
In your example.
with open("s1.txt","r") as f:
x = f.read()
print(not f.closed) # True (it's open)
print(not f.closed) # False (the file has been closed)
If you want o check if file is opened in windows through python you can try this dirty trick. If a file is used in another process which means if a file is opened in winodws we can't rename it.
import os
file = 's1.txt'
if os.path.exists(file):
try:
os.rename(file, file)
print ('File is not opened in windows')
except OSError as e:
print ('File is opened in windows & being used by another program')
Warning: This thing only works in winodws
I'm also adding this answer not sure what is actual aim. If you want to check if file opened in same proccess python code then
with open("s1.txt","r") as f:
x = f.read()
if not f.closed:
print("File is already opened in python")
The psutil library provides a method to discover running processes and check for open files in a given process.
If you want to check that a given process is being opened by a specific version of python (like 3.8.0) you can check that too by checking the version of the python executable using subprocess
(there may be a cleaner way to do this)
import psutil
import subprocess
import os
python_version = '3.8.0'
my_file = 's1.txt'
# iterate over all processes
for proc in psutil.process_iter():
# check if a process name contains the string 'python' (this is not foolproof since you can imagine a scenario where a python process is running under a different name but it's good enough for now)
if 'python' in proc.name():
# get the version of the running python process you found
version = subprocess.run(['python', '-c', 'v = __import__("sys").version_info; print("{}.{}.{}".format(v.major, v.minor, v.micro))'], stdout=subprocess.PIPE, universal_newlines=True).stdout
# check if the version matches the one you're looking for
if version.strip() == python_version:
# check if your file is one of the files opened by the process you found
open_filenames = [f.path for f in proc.open_files()]
if os.path.abspath(my_file) in open_filenames:
print('file {} is open in python {} with PID {}'.format(my_file, python_version, proc.pid))