Reputation: 25
I have a problem with a code i'm writing. One of it's parts is responsible for creating a file, and it is also supposed to tell me if it was successful. If not, it should inform me as well, but the problem is - it checks if the file exists before it gets created. I tried to make a break between creation of file and checking if it exists by using time module (specifically time.sleep option, inserted in almost every place possible), but with no results. I also tried to use another way to check if the file exists. It didn't help. Whole thing works fine (excluding this part), because if the file is already there and i tell the program to create it, it tells me that it was successful, so it can read it properly, but it's too fast. I attached part of my code down below. Thank you in advance.
First way i tried, using os module:
path = 'D:\screen'
os.chdir(path)
exists = os.path.isfile('.\screenshot.png')
exists2 = os.path.isfile('.\screenshot2.png')
And here's the execution part:
def printer():
pyautogui.screenshot('D:\SCREEN\screenshot.png')
time.sleep(3)
if exists:
print("Screenshot was created successfully")
else:
print("Screenshot was not created successfully")
def printer2():
pyautogui.screenshot('D:\SCREEN\screenshot2.png')
time.sleep(3)
if exists2:
print ("Screenshot was created successfully")
else:
print ("Screenshot was not created successfully")
Second way i tried, using pathlib:
path = 'D:/screen'
file1 = Path("D:/screen/screenshot.png")
file2 = Path("D:/screen/screenshot2.png")
And the execution part:
def printer():
pyautogui.screenshot('D:/SCREEN/screenshot.png')
time.sleep(3)
if file1.isfile():
print("Screenshot was created successfully")
else:
print("Screenshot was not created successfully")
def printer2():
pyautogui.screenshot('D:/SCREEN/screenshot2.png')
time.sleep(3)
if file2.isfile():
print("Screenshot was created successfully")
else:
print("Screenshot was not created successfully")
Upvotes: 0
Views: 254
Reputation: 25
Thank you for all of your answers. I was able to deal with my problem by using the solution proposed by Zonyl. It looks like the reason standing behind it was trivial(,,Those variables (file1, file2) were assigned before creating the screenshot" ,,Assuming that the first half of each attempt is executed before the printer functions are called, you are storing the result of os.path.isfile() before you've created the file you are testing.") but i'm thankful you helped me anyway. I hope another newbie in the future may find it useful.
Upvotes: 0
Reputation: 882
Welcome to SO!
The best to check if the file exists or not is using a try/catch
block.The problem in the code is that their is a race condition between the line os.path.isfile('.\screenshot.png')
and the if exists
part.
You can try to use the following -
try:
fh = open('.\screenshot.png', 'rw'):
# Do something
except FileNotFoundError:
print("File not found")
Upvotes: 0
Reputation: 736
I have no idea what pyautogui.screenshot() does but here goes:
Assuming that the first half of each attempt is executed before the printer functions are called, you are storing the result of os.path.isfile() before you've created the file you are testing.
You should also pick a case for the folder name, really it should be in a variable so that you are not typing it twice. You should also use os.path.join instead of typing directory separators.
In printer and printer2 of the first half you should be able to change exists/exists2 to a call to os.path.isfile().
In the simple case this should work:
def printer():
pyautogui.screenshot('D:\screen\screenshot.png') #assume this attempts to create a file
if os.path.isfile('.\screenshot.png'):
print("Screenshot was created successfully")
else:
print("Screenshot was not created successfully")
path = 'D:\screen'
os.chdir(path)
printer()
Upvotes: 0
Reputation: 1208
Those variables (file1
, file2
) were assigned before creating the screenshot, hence they dont exist. screenshot
actually returns a PIL image object. So you can check if without even using os
.
def printscreen():
try:
image = pyautogui.screenshot('D:/SCREEN/screenshot.png')
except Exception as e:
print(f'Exception occured during screenshotring {str(e)}')
If you want to still check with os if they exist, use it after the screenshot.
pyautogui.screenshot('D:/SCREEN/screenshot.png')
assert os.file.exist('D:/SCREEN/screenshot.png')
Upvotes: 1