Type error while working on yolo

In case you do end up using backslashes, you can consider writing raw string literals, which disables backslash escapes and lets you write this:

src = os.path.join(r'C:\Users\Lenovo\Desktop\Internship'
                   r'\YOLO\dataset_pascal_voc', filename)
dst = os.path.join(r'C:\Users\Lenovo\Desktop\Internship'
                   r'\YOLO\dataset_pascal_voc\train', filename)
move(src, dst)

As Karl mentioned though forward slashes should also work on Windows, so you shouldn’t be too concerned about converting them either way :slight_smile: The only difference is that backslashes are native to Windows while forward slashes are compatible with other operating systems, but since you’re running this on Windows anyways I see no problem using either as your path separator.

P.S. Since I see that you are using Jupyter Notebook, I’d like to point out that you can use the cells to test short commands out (like you would in a shell!) In your case of getting a FileNotFoundError, you can just copy out the repr and see what’s missing. Taking your error as FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:/Users/Lenovo/Desktop/Internship/YOLO/dataset_pascal_voc\\2007_000027.jpg' -> 'C:/Users/Lenovo/Desktop/Internship/YOLO/dataset_pascal_voc/train\\2007_000027.jpg', I would test the following (run these uncommented lines in separate cells like you would in Python REPL):

# src should exist
os.path.exists('C:/Users/Lenovo/Desktop/Internship/YOLO/dataset_pascal_voc\\2007_000027.jpg')

# The folder (directory) that contains dst should exist
# (and a directory? I think... idk if on Windows os.rename
# would raise a different exception if it were not one.)
os.path.isdir('C:/Users/Lenovo/Desktop/Internship/YOLO/dataset_pascal_voc/train')

# Either of these giving false could be the cause of the error
# message you see in your jupyter notebook...

If any of these returns false, try pasting the paths into the path bar where your cursor is located here and see if File Explorer understands the path; or you could navigate to that folder within File Explorer, copy the path from the path bar, then compare it with the path given by the error message… That should tell you something, yeah. (By the way, your File Explorer is hiding the file extensions of the JPEG files. You might want to also check if their extensions are really .jpg by checking the “File name extension” box somewhere in your File Explorer settings - again, just in case.)

Or you could make your life easier by running Jupyter from the YOLO folder: just open that folder and type cmd into the path bar then jupyter notebook into the command prompt. That’ll start your notebook server at that directory and relative paths like dataset_pascal_voc/train/2007_000027.jpg should work just fine as well! I don’t know what YOLO stands for but if that’s a place where your notebooks and files both happen to live in… you should give relative paths a shot as that’ll save you a lot of headache if you decide to move your project elsewhere :wink:

In any case, it’s always a good idea to define a base directory like Karl did. Maybe consider defining dataset_pascal_voc = 'C:/Users/Lenovo/Desktop/Internship/YOLO/dataset_pascal_voc' (or simply 'dataset_pascal_voc' if you are using relative paths) so that you can write:

src = os.path.join(dataset_pascal_voc, filename)
dst = os.path.join(dataset_pascal_voc, 'train', filename)
move(src, dst)

This way if you ever rename the directory, you only have to correct your path in one place, instead of having to find and replace every path you’ve used C:/Users/Lenovo/Desktop/Internship/YOLO/dataset_pascal_voc in (that would not be fun, I think :frowning:).

1 Like