Link to home
Start Free TrialLog in
Avatar of Porffor
PorfforFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Long file paths with pywin32 Python library

When attempting to use “excel.Workbooks.Open(filepath)” with pywin32 when the file paths are longer than 259 characters, I get an error saying that the path is too long. It works OK if the path is 254 characters.

With the Pandas library, for example, I've been able to get around the problem by prefixing the file paths with “\\?\”, but unable to do that with pywin32.

I've enabled the following local group policy setting https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=cmd and whilst it did make a difference to Pandas (since then I don't need to use the prefix “\\?\” at all), it's made no difference to pywin32.

Any ideas of how I can get around this (apart from reducing the file path lengths).

Thanks.
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

You can change the window registry setting to allow long path name on Windows system (use regedit.exe):

under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem, set LongPathsEnabled to DWORD value 1.

Open in new window

Avatar of Porffor

ASKER

Hi David,

The Group Policy I mentioned above, that's what this does, it sets the reg key that you mentioned.

So that's already set on the machine.
Are you facing the same error message after edit the group policy to enable the LongPathsEnabled setting?
Furthermore, can i know which python version and Window OS version that you are using there?

Avatar of Porffor

ASKER

Yes same error, enabling the GPO made no difference to pywin32 specifically

Windows 10 Enterprise 1909 10.0.18363.1621
Anaconda3 2020.02 (Python 3.7.6 64-bit)
ASKER CERTIFIED SOLUTION
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Porffor

ASKER

The user that was having this problem no longer has work that involves there long file paths, but the next time they do I have asked them to use the workaround that you suggested. Thanks for the tip.