Type error while working on yolo

TypeError: stat: path should be string, bytes, os.PathLike or integer, not map

 src=os.path.join('C:\\Users\\Lenovo\\Desktop\\Internship\\YOLO\\dataset_pascal_voc', filename)
    src=(map(lambda x: x.replace('\\','/'),src))
    dst=os.path.join('C:\\Users\\Lenovo\\Desktop\\Internship\\YOLO\\dataset_pascal_voc\\train', filename)
    dst=(map(lambda x: x.replace('\\','/'),dst))

what to do?

Where the code says:

Exactly what do you expect this to do, and how? What do you expect src to contain before and after this line?

If you expected to turn a list of strings into a list of strings, then you are way off base. First off, map gives a special iterator now, not a list. Second, you got src from os.path.join, which just gives back a single string (the joined-up path). Third, you cannot shutil.move from a list to a list. It wants a single string for the source and the destination.

If you expected to turn a string into another string, then none of this map and lambda stuff makes any sense. That tries to loop over each character of the string (treating it as a separate substring) to do the replacement. But the entire point of having a replace method for a string is that you can do it all at once. What you want is much more simple: src = src.replace('\\', '/'). Similarly for dst.

But you don’t actually need to do this at all. The path with backslashes in it will work just fine. And if you wanted to use forward slashes, then you should have just used them in the first place, like 'C:/Users/Lenovo/Desktop/Internship/YOLO/dataset_pascal_voc'. That also works just fine. And you can even mix and match. When people suggest using forward slashes instead of backslashes, that’s so you don’t have to worry about the escaping (doubling up \\ to represent a single backslash in the actual string).

Even better is to use the standard library pathlib to create the paths. You can write it much more simply and elegantly:

from pathlib import Path
base_dir = Path('C:/Users/Lenovo/Desktop/Internship/YOLO/dataset_pascal_voc')
move(base_dir / filename, base_dir / 'train' / filename)

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

# 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.)

# 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:).