user28761793
user28761793

Reputation: 1

Selective OS package error in Python (Windows 10)

I have an error where the os package seems to only be able to detect some folders and not others. My code is at the bottom; when I run it, I get the following outputs: "Bro reads", "It's not real", and "Error creating folder". D:\Books and D:\243labshit are both real folders on my computer. I tried creating D:\243labshit\bruh1 manually using the File Explorer to see if I'd see "It's real" in the output, but the output looked exactly the same. It was also unchanged when I deleted it.

Something else to note is that I did not originally have cv2 (OpenCV) installed; I did this manually. Before doing this, I was not getting any OSErrors, but I had to comment out all the lines referencing cv2 or else I'd get the ModuleNotFoundError: No module named error. After installing it, I stopped getting this error, but os stopped being able to detect D:\243labshit\bruh1 for some reason. When I installed it, it initially created two folders in my C:\ drive, but I moved those to D:\Programs\anaconda3\Lib\site-packages where everything else like numpy and matplotlib already are. Also, regardless of whether ...\bruh1 exists or not, cv2 doesn't create the image files. For reference, I tried following section 3.2.2 from this guide: https://www.geeksforgeeks.org/opencv-python-tutorial/#1-getting-started

Does anyone know what could be causing this? I'm guessing I might've installed something incorrectly, but I don't understand why that'd affect os, and why os is only unable to detect D:\243labshit. It can also detect folders in C:\ and my third drive, B:\.

Any help would be much appreciated

import cv2
import os

if __name__ == "__main__":
    v1_file = "D:\243labshit\bubble914.mp4"
    video1 = cv2.VideoCapture(v1_file)

    try:
        if os.path.exists("D:\Books"):
            print("Bro reads")
        else:
            print("Bro is illiterate")
    except OSError:
        print("Books aren't real")

    try:
        if not os.path.exists("D:\243labshit\bruh1"):
            print("It's not real")
            os.makedirs("D:\243labshit\bruh1")
            print("It's real now")
        else:
            print("It's real")
            
    except OSError:
        print("Error creating folder")
    
    currentframe = 0
    while(True):
        ret,frame = video1.read()
        if ret:
            name = "D:\243labshit\bruh1\frame" + str(currentframe) + ".png"
            print("Creating " + name)
            cv2.imwrite(name, frame)
            currentframe += 1
        else:
            break
    video1.release()
    cv2.destroyAllWindows()

Upvotes: -1

Views: 43

Answers (1)

aperson
aperson

Reputation: 51

Put an r in front of the string for the file path to make the path raw. This is because many commands are associated with \, such as \n to make a new line. By putting r in front, it prevents any of these commands from going through. This is shown here:

print("D:\243labshit\bruh1\frame")
print("\n\n")
print(r"D:\243labshit\bruh1\frame")

which outputs:

D:£labshiruh1
rame



D:\243labshit\bruh1\frame

The first one is what happens when you do not add the r in front of the string and the second one is what happens when you do. Currently, your computer is trying to find a file path under the first one.

Upvotes: 0

Related Questions