pythonfilefilenotfoundexception

Python no such file or directory, even if it does exist


i am trying to save an image to a directory, but i get no such file or directory even thou its there, i make sure its created at the beginning of the script and i check the folder to see if its there:

my code:

image_content = image_response.content
image_filename = sanitize_filename(item['caption'])

# Get the gallery folder
gallery_folder = sanitize_filename(soup.find(class_="mw-page-title-main").text)
gallery_folder_path = os.path.normpath(os.path.join('gallery', gallery_folder))

# Make sure the directory exists before saving the image
os.makedirs(gallery_folder_path, exist_ok=True)

# Create the full image path using os.path.join() with gallery_folder_path
image_path = os.path.join(gallery_folder_path, f'{image_filename}.jpg')

# Print the values for debugging
print(f"image_filename: {image_filename}")
print(f"gallery_folder: {gallery_folder}")
print(image_path)
input()
with open(image_path, 'wb') as file:
    file.write(image_content)

the console output:

image_filename: The_Farm_by_Jean-Baptiste_Oudry_1750_oil_on_canvas_130_x_212_cm_Louvre._This_Rococo_painting_shows_how_Cottagecore_visuals_are_a_continuation_of_depictions_of_the_traditional_French_and_English_countryside
gallery_folder: Cottagecore
gallery\Cottagecore\The_Farm_by_Jean-Baptiste_Oudry_1750_oil_on_canvas_130_x_212_cm_Louvre._This_Rococo_painting_shows_how_Cottagecore_visuals_are_a_continuation_of_depictions_of_the_traditional_French_and_English_countryside.jpg

Traceback (most recent call last):
  File "C:\Users\PC GAMER\Desktop\Devs\python\scrappers\tvtropes_scrapper\test.py", line 76, in <module>
    with open(image_path, 'wb') as file:
FileNotFoundError: [Errno 2] No such file or directory: 'gallery\\Cottagecore\\The_Farm_by_Jean-Baptiste_Oudry_1750_oil_on_canvas_130_x_212_cm_Louvre._This_Rococo_painting_shows_how_Cottagecore_visuals_are_a_continuation_of_depictions_of_the_traditional_French_and_English_countryside.jpg'

I use a sanitize function to clean the file name:

def sanitize_filename(filename):
    # Remove invalid characters from the filename
    valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
    sanitized_filename = ''.join(c for c in filename if c in valid_chars)
    sanitized_filename = sanitized_filename.replace('/', '_')  # Replace forward slash with underscore
    sanitized_filename = sanitized_filename.replace(' ', '_')  # Replace spaces with underscores
    return sanitized_filename

I had been bashing my head against this error for a while now, i know it seems simple but i couldnt figure it out, what could the isssue be here ?

Thanks in advance


Solution

  • The issue here was the file path length, there is limit to it, which is a total of 260 characters.

    here is the fix:

    image_content = image_response.content
                                image_filename = sanitize_filename(item['caption'])
    
                                # Get the gallery folder
                                gallery_folder = sanitize_filename(soup.find(class_="mw-page-title-main").text)
                                gallery_folder_path = os.path.normpath(os.path.join('gallery', gallery_folder))
    
                                # Make sure the directory exists before saving the image
                                os.makedirs(gallery_folder_path, exist_ok=True)
    
                                # Check if the combined length of directory path and file name is too long
                                max_path_length = 259  # Maximum path length for most Windows systems
                                combined_length = len(
                                    os.path.join(get_script_directory(), gallery_folder_path, f'{image_filename}.jpg'))
                                if combined_length > max_path_length:
                                    # Shorten the image_filename to fit within the limit
                                    max_filename_length = max_path_length - len(
                                        os.path.join(get_script_directory(), gallery_folder_path, '')) - len('.jpg')
                                    image_filename = image_filename[:max_filename_length]
    
                                # Create the full image path using os.path.join() with gallery_folder_path
                                image_path = os.path.join(get_script_directory(), gallery_folder_path, f'{image_filename}.jpg')
                                image_path = image_path.replace('\\', '/')
    
                                # Print the values for debugging
                                print(f"image_filename: {image_filename}")
                                print(f"gallery_folder: {gallery_folder}")
                                print('full path:'+image_path)