file names longer than MAX_PATH under Windows 2003

S

Sergey

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?
 
S

Steven D'Aprano

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.

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?
 
S

Sergey

Steven D'Aprano said:
f = file("E:/files/.../something.dat", "r")
Does this help?

backslashes quoted.
\\.\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)
Traceback (most recent call last):
,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?
 
S

Steven D'Aprano

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")
 
S

Sergey

Steven D'Aprano said:
Are you passing a unicode object to the function?

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

I pass variable c into functions:
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'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top