Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > os.stat() distorts filenames that end with period (nt.stat())

Reply
Thread Tools

os.stat() distorts filenames that end with period (nt.stat())

 
 
ruck
Guest
Posts: n/a
 
      09-07-2012
On Thursday, September 6, 2012 7:05:39 PM UTC-7, Terry Reedy wrote:
> On 9/6/2012 8:55 PM, ruck wrote:
>
> > (This with Python 2.7.2 on Windows 7)

>
> >

>
> > os.stat() won't recognize a filename ending in period.

>
> > It will ignore trailing periods.

>
> > If you ask it about file 'goo...' it will report on file 'goo'

>
> > And if 'goo' doesn't exist, os.stat will complain.

>
> >

>
> > create file goo, then

>
> >

>
> > >>> os.stat('goo')

>
> > nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=0L, st_atime=1346978160L, st_mtime=1346978160L, st_ctime=1346978160L)

>
> > >>> os.stat('goo...')

>
> > nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=0L, st_atime=1346978160L, st_mtime=1346978160L, st_ctime=1346978160L)

>
> >

>
> > rename goo to "goo...", then,

>
> >

>
> > >>> os.stat('goo...')

>
> >

>
> > Traceback (most recent call last):

>
> > File "<pyshell#16>", line 1, in <module>

>
> > os.stat('goo...')

>
> > WindowsError: [Error 2] The system cannot find the file specified: 'goo...'

>
> >

>
> > Puzzling, to me at least.

>
> > Any comments?

>
>
>
> Windows have restrictions on filenames. The restrictions are not
>
> consistent in that some parts of Windows will let you make names that
>
> other parts do not recognize or regard as illegal. I ran into this some
>
> years ago and there may be a discussion on the tracker, but I have
>
> forgetten the details except that one of the 'parts' was Windows
>
> Explorer. This *might* be what you are running into.
>
>
>
> --
>
> Terry Jan Reedy


Summary:
I was complaining that
>>> os.stat('goo...')


Traceback (most recent call last):
File "<pyshell#35>", line 1, in <module>
os.stat('goo...')
WindowsError: [Error 2] The system cannot find the file specified: 'goo...'

is actually looking for file 'goo' instead of the existing file 'goo...' -- that's why it errors.
(Python 2.7.2, Windows 7)

Here's a workaround.
>>> os.stat('\\\\?\\' + os.getcwd() + '\\' + 'goo...')

nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0,
st_gid=0, st_size=0L, st_atime=1346978160L, st_mtime=1346978160L,
st_ctime=1346978160L)

In other words, prefix the path (full path?) with \\?\ to disable the Windows API filename interceptions.

Detail:
"Naming Files, Paths, and Namespaces" ( http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx )
says

Naming Conventions
The following fundamental rules enable applications to
create and process valid names for files and directories,
regardless of the file system:

* Do not end a file or directory name with a space or a
period. Although the underlying file system may support
such names, the Windows shell and user interface does not.

In my case, the filesystem is NTFS and it does support filenames like 'goo...', and I'm not trying to use Windows shell or UI. So how do I get to the underlying filesystem without interference? Here (from the same reference)

Namespaces
There are two main categories of namespace conventions
used in the Windows APIs, commonly referred to as NT
namespaces and the Win32 namespaces. ...

Win32 File Namespaces
The Win32 namespace prefixing and conventions are
summarized in this section and the following section,
with descriptions of how they are used. Note that these
examples are intended for use with the Windows API
functions and do not all necessarily work with Windows
shell applications such as Windows Explorer. For this
reason there is a wider range of possible paths than is
usually available from Windows shell applications, and
Windows applications that take advantage of this can be
developed using these namespace conventions.

For file I/O, the "\\?\" prefix to a path string tells
the Windows APIs to disable all string parsing and to
send the string that follows it straight to the file
system. For example, if the file system supports large
paths and file names, you can exceed the MAX_PATH limits
that are otherwise enforced by the Windows APIs.

Thanks all for your comments and pointers.
John
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Not wide-angle but still distorts Charles Packer Digital Photography 3 04-09-2008 10:47 AM
VoIPCheap/Stunt/SIPDiscount/Et.al - Mobile - Top-up Expiry Period -- Campaign for Correct Expiry Period on Finarea VOIP Service Mobile Top-Ups News Reader UK VOIP 16 06-26-2006 05:03 PM
problem with filenames, Filenames and FILENAMES B.J. HTML 4 04-23-2005 08:13 PM
Word grammar checker doesn't catch sentences that do not end in a period Ima Goodlay Computer Support 3 10-08-2003 11:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57