Title: Can python3 vs python make a difference when renaming and opening files in pyautogui?
Tags: python,python-3.x,automation,subprocess,pyautogui

Question: I have a python script that is supposed to take a screenshot of each .txt file in a directory and add with a .png extension. Except it seemingly misnames the file it is supposed to be taking a screenshot of. The files are sequentially numbered as they are frames for an animation. Right now 1.txt can be misnamed as 3.png. Here's the script:

import os
import pyautogui
import subprocess

def process_text_file(filename):

    subprocess.call(["open", "-a", "TextEdit", filename])
    
    pyautogui.sleep(1)
    
    for i in range(10):
        pyautogui.hotkey("command", "-")
    
    pyautogui.hotkey("ctrl", "command", "f")
    
    for i in range(4):
        pyautogui.keyDown('shift')
        pyautogui.keyDown('command')
        pyautogui.press('.')
        pyautogui.keyUp('shift')
        pyautogui.keyUp('command')
    
    pyautogui.hotkey("command", "up")
    
    pyautogui.sleep(1)
    
    screenshot = pyautogui.screenshot()
    
    pyautogui.sleep(1)
    
    screenshot_filename = os.path.splitext(filename)[0] + ".png"
    screenshot.save(screenshot_filename)
    
    pyautogui.hotkey("command", "w")

    filename = "00.jpg.txt"

for filename in os.listdir(os.getcwd()):
if filename.endswith(".txt"):
process_text_file(filename)

I also have an issue when it opens the first file - it zooms in too much - but the naming issue is priority for me right now.
I tried putting in more pyautogui.sleep(1) to no avail. I've also tried taking the for loop out at the end and running the script in a for loop in terminal like so:
 for file in *.txt; do  python3 script.py $file done
That also didn't work.
I've additionally tried
file_list = natsorted(\[filename for filename in os.listdir(os.getcwd()) if filename.endswith(".txt")\])

for filename in file_list:

    process_text_file(filename)

Edit: I've found that one of my computers does not have this problem, but the one I primarily use does. The one that works uses the python command and version 3.9.12 to run the script and the other uses the python3 command and version 3.9.6. Could that make a difference?
Update: All discrepancies with dependencies were corrected and the computers run on the same OS and processor (Mac Ventura, M1).
Update: Putting in  screenshot = pyautogui.screenshot() after the script saves the new file and after it closes the program helped!!!

Answer: In general, the Python version (Python 2 vs. Python 3) should not directly impact the behavior of file renaming and opening in PyAutoGUI. However, there are a few aspects to consider when it comes to file operations that could potentially lead to differences in behavior:
File system differences: It's possible that the two computers have different file systems or file naming conventions, which could affect how files are handled. For example, if one computer has a case-sensitive file system and the other has a case-insensitive file system, it could lead to issues with file naming.
Operating system differences: The behavior of file operations can vary between different operating systems. If the two computers are running different operating systems, it's possible that the differences in behavior are due to the underlying operating system rather than the Python version itself.
Dependent libraries: PyAutoGUI relies on various underlying libraries to interact with the operating system and perform GUI automation tasks. Differences in the versions of these libraries between the two computers could potentially lead to variations in behavior.