Created
June 15, 2019 20:26
-
-
Save alkuzad/46a12a96b837c2b5537ed7627b67ad7d to your computer and use it in GitHub Desktop.
pywin32 read file that exceeds 260chars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import win32file | |
| def read260(path): | |
| handle = None | |
| try: | |
| # http://docs.activestate.com/activepython/2.6/pywin32/win32file__CreateFileW_meth.html | |
| # http://timgolden.me.uk/pywin32-docs/win32file__CreateFileW_meth.html | |
| handle = win32file.CreateFileW( | |
| path, | |
| win32file.GENERIC_READ, | |
| 0, # SHARE_MODE - 0 = no share (reserve for this process) | |
| None, # SecurityAttributes | |
| win32file.OPEN_EXISTING, # CreationDisposition | |
| win32file.FILE_ATTRIBUTE_NORMAL, #FlagsAndAttributes - we read so we do not modify anything | |
| None, #TemplateFile - not needed for read | |
| None, #Transaction - optional, use win32transaction.CreateTransaction + win32transaction.CommitTransaction | |
| None, #MiniVersion - for transaction | |
| None #ExtendedParameter - reserved, only None | |
| ) | |
| content = '' | |
| chunk = None | |
| while chunk != b'': | |
| ret, chunk = win32file.ReadFile(handle, 4096) # http://timgolden.me.uk/pywin32-docs/win32file__ReadFile_meth.html | |
| content += chunk.decode('utf-8') | |
| return content | |
| finally: | |
| if handle is not None: | |
| handle.close() | |
| if __name__ == "__main__": | |
| print(read260('\\\\?\\C:\\Path\\to\\file\\exceeds\\260chars')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment