0

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

Swifty
  • 2,630
  • 2
  • 3
  • 21
  • It looks like maybe you're running into an issue with escaped backslashes here: `'gallery\\Cottagecore\\...'` – JRiggles Aug 23 '23 at 14:07
  • This is the reason I never use `os.path.join` buy simply `'/'.join()` (which works under Win10). – Swifty Aug 23 '23 at 14:24
  • 1
    @JRiggles escaped backslashes are one way to write paths on windows https://stackoverflow.com/a/2953843/5012099 – FlyingTeller Aug 23 '23 at 14:28
  • FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/PC GAMER/Desktop/Devs/python/scrappers/tvtropes_scrapper/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' as you can the path is now correct after i fixed the baskslashes issue, the directory exist but still i get this issue – Adam Demo_Fighter Aug 23 '23 at 14:32
  • 2
    Maybe your absolute path is getting to long? Can you try the same code with a very short filename isntead of the extra long one? – FlyingTeller Aug 23 '23 at 14:33
  • @AdamDemo_Fighter from what I can tell, your path was absolutely fine from the begining. Using `os.path.join` is a good cross-platform approach to built a path as you don't need to worry about the directory seperator – FlyingTeller Aug 23 '23 at 14:34
  • https://docs.python.org/3/using/windows.html#removing-the-max-path-limitation – FlyingTeller Aug 23 '23 at 14:35
  • you are correct sir the issue was the file path length, i minimized the length to 259 max length and now it works, thanks. – Adam Demo_Fighter Aug 23 '23 at 14:38

1 Answers1

0

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)
  • There is an indentation issue in the code. – FlyingTeller Aug 23 '23 at 14:43
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 25 '23 at 23:45