windows and file names > 256 bytes
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Thu Jun 25 05:16:31 EDT 2015
On Thursday 25 June 2015 18:00, Albert-Jan Roskam wrote:
> Hi,
>
> Consider the following calls, where very_long_path is more than 256 bytes:
> [1] os.mkdir(very_long_path)
> [2] os.getsize(very_long_path)
> [3] shutil.rmtree(very_long_path)
>
> I am using Python 2.7 and [1] and [2] fail under Windows XP [3] fails
> under Win7 (not sure about XP). It throws: “WindowsError: [Error 206] The
> filename or extension is too long”
I don't think this is a bug. It seems to be a limitation of Windows.
https://msdn.microsoft.com/en-
us/library/windows/desktop/aa365247%28v=vs.85%29.aspx#maxpath
> This is even when I use the "special"
> notations \\?\c:\dir\file or \\?\UNC\server\share\file, e.g.
> os.path.getsize("\\\\?\\" + "c:\\dir\\file")
However, that may be a bug.
What happens if you use a Unicode string?
path = u"\\\\?\\c:a\\very\\long\\path"
os.mkdir(path)
Can you open an existing file?
open(u"\\\\?\\c:a\\very\\long\\path\\file.txt")
> (Oddly, os.path.getsize(os.path.join("\\\\?", "c:\\dir\\file")) will
> truncate the prefix)
That's worth reporting as a bug.
> My questions:
> 1. How can I get the file size of very long paths under XP?
If all else fails:
last = os.getcwd()
try:
os.chdir('C:/a/very/long')
os.chdir('path/with/many')
os.chdir('nested/folders')
os.path.getsize('/and/even/more/file.txt')
finally:
os.chdir(last)
> 2. Is this a bug in Python? I would prefer if Python dealt with the gory
> details of Windows' silly behavior.
I would say that it is a bug that it doesn't work with extended-length paths
(those starting with \\?\) but may or may not be a bug with regular paths.
--
Steve
More information about the Python-list
mailing list