Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

file names longer than MAX_PATH under Windows 2003

64 views
Skip to first unread message

Sergey

unread,
Feb 14, 2006, 6:29:44 AM2/14/06
to
Hello.

I try to open file with pathname length 282 bytes:
E:\files\..................\something.dat

On MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/naming_a_file.asp) described method to access
files with path length
up to 32000 bytes: just add prefix \\?\ to file name.
But when I try to pass prefixed name to file(), I get the same result as when I don't add the prefix: file not found. May be Python
just doesn't support long unicode filenames?
Is there way to open such files?


Tim Golden

unread,
Feb 14, 2006, 6:43:34 AM2/14/06
to pytho...@python.org
[Sergey]

| I try to open file with pathname length 282 bytes:
| E:\files\..................\something.dat

| [... MS advise ...] just add prefix \\?\ to file name.


| But when I try to pass prefixed name to file(), I get the
| same result as when I don't add the prefix: file not found.

With a file called c:\temp\test.txt, I successfully
opened and read it like this:

print open (r"\\?\C:\temp\test.txt").read ()

So the basic functionality works. I didn't
artificially generate a long path to see if
there's a problem in that direction.

But note that r prefix to the string. Is it possible
that your string didn't include it? If not, then the
backslash character which Windows uses as a separator
can be stolen by Python which sees it as an escaping
character.

TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

Steven D'Aprano

unread,
Feb 14, 2006, 6:43:50 AM2/14/06
to

Backslashes have special meaning to Python and need to be escaped. If you
do this:

f = file("E:\files\...\something.dat", "r")

Python's string escape rules means you are actually trying to open the
file "E:files...something.dat" which doesn't exist.

You should escape the backslashes:

f = file("E:\\files\\...\\something.dat", "r")

or use raw strings:

f = file(r"E:\files\...\something.dat", "r")

or just use forward slashes and let Windows deal with it:

f = file("E:/files/.../something.dat", "r")


Does this help?

--
Steven.

Sergey

unread,
Feb 14, 2006, 6:52:49 AM2/14/06
to

"Tim Golden" <tim.g...@viacom-outdoor.co.uk> wrote in message news:mailman.1895.1139917...@python.org...
[Sergey]

>But note that r prefix to the string. Is it possible
>that your string didn't include it? If not, then the
>backslash character which Windows uses as a separator
>can be stolen by Python which sees it as an escaping
>character.

It's ok with backslashes, I don't use r'', but double them so string is correct:
>>> print c
\\?\e:\files\........


Tim Golden

unread,
Feb 14, 2006, 7:00:38 AM2/14/06
to pytho...@python.org
[Sergey]

Not to state the obvious, but can you cut-and-paste that long
string (the one starting with \\?\e:\...) from the Python
interpreter into the [S]tart [R]un [O]pen field to see what
comes up? I'm just trying to make sure of the most straightforward
fact: that the file you've got there definitely does exist!

Sergey

unread,
Feb 14, 2006, 7:02:48 AM2/14/06
to

"Steven D'Aprano" <st...@REMOVETHIScyber.com.au> wrote in message news:pan.2006.02.14....@REMOVETHIScyber.com.au...

> f = file("E:/files/.../something.dat", "r")
> Does this help?

backslashes quoted.

>>> print c
\\.\e:\files\xxxxxxxxxxxxxxxx\xxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxx\xxxxxxxxxxx xxxxxxxxx
\xxxxxxxxxxxxxxxxxxxxxxx\xxxxxxxxxxxxxx\xxxxxxxxxxxxxxxxxxxxx\xxxxxxxxxxxx
xxxxxxxxxxxxxxx\xxxxxxxxxxxxxxxxxxxxxxxxx\xxxxxxxxxxxxxxxxxxxxxxx\xxxxxx xxxxxxxxxx\xxxxxxxxxxxxxxxxxxxxxxxxxxxx.xls

(cyrillic letters in filename here I replaced with x-es)

>>> file(c,"rb")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 2] No such file or directory: u'\\\\.\\e:\\files\\\u041f\u0420....filename skipped....\u0442.xls'

>>> win32file.CreateFile(c,win32file.GENERIC_READ,0,None,win32file.OPEN_EXISTING
,0,None)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
pywintypes.error: (206, 'CreateFile', 'The filename or extension is too long.')

When I open explorer, I can browse folder and see this file (but I can do almost nothing with it). It was created through share.
Ntbackup can work with the file - why Python can't?


Steven D'Aprano

unread,
Feb 14, 2006, 7:09:43 AM2/14/06
to
On Tue, 14 Feb 2006 22:43:50 +1100, Steven D'Aprano wrote:

> On Tue, 14 Feb 2006 14:29:44 +0300, Sergey wrote:
>
>> Hello.
>>
>> I try to open file with pathname length 282 bytes:
>> E:\files\..................\something.dat
>>
>> On MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/naming_a_file.asp) described method to access
>> files with path length
>> up to 32000 bytes: just add prefix \\?\ to file name.
>> But when I try to pass prefixed name to file(), I get the same result as when I don't add the prefix: file not found. May be Python
>> just doesn't support long unicode filenames?
>> Is there way to open such files?
>
> Backslashes have special meaning to Python and need to be escaped. If you
> do this:
>
> f = file("E:\files\...\something.dat", "r")
>
> Python's string escape rules means you are actually trying to open the
> file "E:files...something.dat" which doesn't exist.

[slaps head]
I seem to be a bit confused about string escaping rules. Only some
backslashes have special meaning, the rest remain in the string.

Sergey, you said UNICODE file names. Not just ordinary strings.

Are you passing a unicode object to the function?

f = file(u"E:\\files\\...\\something.dat", "r")


--
Steven.

Sergey

unread,
Feb 14, 2006, 7:10:22 AM2/14/06
to

"Tim Golden" <tim.g...@viacom-outdoor.co.uk> wrote in message news:mailman.1897.1139918...@python.org...
[Sergey]

>Not to state the obvious, but can you cut-and-paste that long
>string (the one starting with \\?\e:\...) from the Python
>interpreter into the [S]tart [R]un [O]pen field to see what
>comes up? I'm just trying to make sure of the most straightforward
>fact: that the file you've got there definitely does exist!

I cannot do this: when I paste filename there, trail of filename is missing due to length limit in input line.
But I strongly suppose that file exists as its name was get through os.listdir.


Sergey

unread,
Feb 14, 2006, 7:12:37 AM2/14/06
to

"Steven D'Aprano" <st...@REMOVETHIScyber.com.au> wrote in message news:pan.2006.02.14....@REMOVETHIScyber.com.au...
> Are you passing a unicode object to the function?
>
> f = file(u"E:\\files\\...\\something.dat", "r")

I pass variable c into functions:

>>> c
u'\\\\.\\e:\\files\\\u041f\u0420\u041e\u0414\u041e \u041c\u0435\u043d\u0435\u043
[many unicode chars skipped]
44b\u0439_\u0411\u044e\u0434\u0436\u0435\u0442.xls'


Sergey

unread,
Feb 14, 2006, 7:17:36 AM2/14/06
to

"Tim Golden" <tim.g...@viacom-outdoor.co.uk> wrote in message news:mailman.1897.1139918...@python.org...
[Sergey]

>Not to state the obvious, but can you cut-and-paste that long
>string (the one starting with \\?\e:\...) from the Python
>interpreter into the [S]tart [R]un [O]pen field to see what
>comes up? I'm just trying to make sure of the most straightforward
>fact: that the file you've got there definitely does exist!

Sorry, it was my error - there was extra backslash in the middle of path.
\\?\path works OK.


Tim Golden

unread,
Feb 14, 2006, 8:53:47 AM2/14/06
to pytho...@python.org
[Sergey]

| "Tim Golden" <tim.g...@viacom-outdoor.co.uk> wrote in
| message news:mailman.1897.1139918...@python.org...
| [Sergey]
|
| >Not to state the obvious, but can you cut-and-paste that long
| >string (the one starting with \\?\e:\...) from the Python
| >interpreter into the [S]tart [R]un [O]pen field to see what
| >comes up? I'm just trying to make sure of the most straightforward
| >fact: that the file you've got there definitely does exist!
|

| I cannot do this: when I paste filename there, trail of
| filename is missing due to length limit in input line.
| But I strongly suppose that file exists as its name was get
| through os.listdir.

I see from another post that CreateFile cannot open your file.
That puts it further away from Python, although it doesn't
explain how some other program can see the files. Can you use
os.startfile (or its equivalent win32api.ShellExecute from
pywin32)? Perhaps if you were to chdir to the directory in
question you'd be able to access the file.

Don't know really; clutching at straws.

Sergey

unread,
Feb 15, 2006, 10:40:54 AM2/15/06
to

"Tim Golden" <tim.g...@viacom-outdoor.co.uk> wrote in message news:mailman.1900.1139925...@python.org...
[Sergey]

>I see from another post that CreateFile cannot open your file.
>That puts it further away from Python, although it doesn't
>explain how some other program can see the files. Can you use
>os.startfile (or its equivalent win32api.ShellExecute from
>pywin32)? Perhaps if you were to chdir to the directory in
>question you'd be able to access the file.

I found error in filename (extra backslash) and now I can open file.
But another trouble appeared: I cannot listdir so long name.
I examined posixmodule.c... There are wcsncpy with hard limit of MAX_PATH*2+5.
So... RIP, my module...


Claudio Grondi

unread,
Feb 15, 2006, 10:52:30 AM2/15/06
to
I don't know if and how it apply and can be of any help here, but in my
C programs in the very past after switching from DOS to Windows long
names a following trick solved often my problems with too long names:
step 1: setting the current directory to the beginning part of the
name with a length which can be passed as a function parameter
step 2: usage of the remaining part of name as name passed as
parameter to the file accessing function (relative to current directory)

Claudio

Tim Golden

unread,
Feb 15, 2006, 11:22:14 AM2/15/06
to pytho...@python.org
[Sergey]

| "Tim Golden" <tim.g...@viacom-outdoor.co.uk> wrote in
| message news:mailman.1900.1139925...@python.org...
| [Sergey]
|
| >I see from another post that CreateFile cannot open your file.
| >That puts it further away from Python, although it doesn't
| >explain how some other program can see the files. Can you use
| >os.startfile (or its equivalent win32api.ShellExecute from
| >pywin32)? Perhaps if you were to chdir to the directory in
| >question you'd be able to access the file.
|
| I found error in filename (extra backslash) and now I can open file.
| But another trouble appeared: I cannot listdir so long name.
| I examined posixmodule.c... There are wcsncpy with hard limit
| of MAX_PATH*2+5.
| So... RIP, my module...

Have a look at win32file.FindFilesIterator from the pywin32 extensions.
Maybe that can cope? (I haven't looked at the source).

<code>
import win32file
for (
attr, created, accessed, written, size_hi, size_lo,
_, _,
filename, alt_filename
) in win32file.FindFilesIterator ("c:/temp/*"):
print filename

</code>

Sergey

unread,
Feb 15, 2006, 11:29:57 AM2/15/06
to

"Tim Golden" <tim.g...@viacom-outdoor.co.uk> wrote in message news:mailman.1956.1140020...@python.org...
[Sergey]


>Have a look at win32file.FindFilesIterator from the pywin32 extensions.
>Maybe that can cope? (I haven't looked at the source).

Yeah, it works!
THANK YOU!
(but now I must have two pieces of code, one for linux and one for windows)
Interesting, why developers of python didn't use here all power of win32 API?


Sergey

unread,
Feb 15, 2006, 11:52:42 AM2/15/06
to

"Claudio Grondi" <claudio...@freenet.de> wrote in message news:dsvims$gps$1...@newsreader3.netcologne.de...

> Sergey wrote:
> I don't know if and how it apply and can be of any help here, but in my C programs in the very past after switching from DOS to
> Windows long names a following trick solved often my problems with too long names:
> step 1: setting the current directory to the beginning part of the name with a length which can be passed as a function
> parameter
> step 2: usage of the remaining part of name as name passed as parameter to the file accessing function (relative to current
> directory)

It seems this method doesn't help here.


"Martin v. Löwis"

unread,
Feb 15, 2006, 2:40:36 PM2/15/06
to Sergey
Sergey wrote:
> (but now I must have two pieces of code, one for linux and one for windows)
> Interesting, why developers of python didn't use here all power of win32 API?

It will in Python 2.5.

Regards,
Martin

0 new messages