Error if full path to file more than 256 symbols

Hi, everyone again.
I’ve made code for get list files in folder and subfolders.
It’s working if full path to the file less than 256 symbols.
If full path more than 256 symbols ( (Windows issue). ), I would get error “FileNotFoundError: [WinError 3]” System can’t find file.
Could you please healp me how to avoid it ???
import os
import csv
directory = r"y:\GIRS"

with open(‘C:/Temp/20_MDG_Congo_MarineXII/opa2.csv’, ‘w’) as f:
write = csv.writer(f)
for root, dirs, files in os.walk(directory):
for file in files:
result = (file,os.path.join(root,file),os.stat(os.path.join(root,file)).st_size)
print (result)

As you implied, this is a limitation of Windows itself, which is kept to preserve backwards compatibility with programs. But Python is compiled to be able to handle longer paths, if you enable the relevant registry setting. After that your program will just work.

Dear Spencer,
Thank you so much.
Your suggest is correct for Windows 10 or 11.
I have Windows 7 and there isn’t working in section “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem”
I can change code ??? I don’t know how to do it.

The registry setting is not available in windows 7:

Starting in Windows 10, version 1607, MAX_PATH limitations have been removed from common Win32 file and directory functions.

However, this answer on Stack Overflow to the question “Pathname too long to open?” gives a workaround and includes a thorough explanation.

The limit is 257 made up of 2 for device “c:” etc the 255 for the path.
Which comes from the maximum size of a pascal string that used a single byte for its length.
Isn’t windows cute?

To be fair, that’s on par with how the size of the Space Shuttle’s solid rocket boosters were governed by the width of two horses’ behinds. The original reason for it is really just history, and it’s been the maintenance of backward compatibility and standards that have kept it the same ever since.

Just to make sure: you understand that Microsoft has not provided any kind of support for Windows 7, for almost four years now?