Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28898

Re: how to get os.py to use an ./ntpath.py instead of Lib/ntpath.py

Newsgroups comp.lang.python
Date 2012-09-11 12:13 -0700
References <a4a2e63b-d25d-4487-bff8-67d4b7c40cbc@googlegroups.com> <504e4a8d$0$29981$c3e8da3$5496439d@news.astraweb.com> <83af64e3-bc26-4217-8afa-e4f6d45b604d@googlegroups.com> <504eb3fc$0$29890$c3e8da3$5496439d@news.astraweb.com> <mailman.489.1347348083.27098.python-list@python.org>
Message-ID <9ef20f77-487f-4250-91af-5d7d2491da05@googlegroups.com> (permalink)
Subject Re: how to get os.py to use an ./ntpath.py instead of Lib/ntpath.py
From ruck <john.ruckstuhl@gmail.com>

Show all headers | View raw


On Tuesday, September 11, 2012 12:21:24 AM UTC-7, Tim Golden wrote:
> And so it does, but you'll notice from the MSDN docs that the \\?
> syntax must be supplied as a Unicode string, which os.listdir
> will do if you pass it a Python unicode object and not otherwise:

I was saying os.listdir doesn't like the r'\\?\' prefix.
But Tim corrects me -- so yes, Steven's earler suggestion "Why don't you just prepend a '?' to paths like they tell you to?" does work, when I supply it in unicode.
Good:
    >>> os.listdir(u'\\\\?\\C:\\Users\\john\\Desktop\\sandbox\\goo')
   [u'voo...']
Bad:
    >>> os.listdir('\\\\?\\C:\\Users\\john\\Desktop\\sandbox\\goo')

    Traceback (most recent call last):
      File "<pyshell#3>", line 1, in <module>
        os.listdir('\\\\?\\C:\\Users\\john\\Desktop\\sandbox\\goo')
    WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: '\\\\?\\C:\\Users\\john\\Desktop\\sandbox\\goo/*.*'

Thanks to both of you for taking the time to teach.

BTW, when I posted the original, I was trying to supply my own customized ntpath module, and I was really puzzled as to why it wasn't getting picked up! According to sys.path I expected my custom ntpath.py to be chosen, instead of the standard Lib/ntpath.py.

Now I guess I understand why.  I moved Lib/ntpath.* out of the way, and learned that during initialization, Python is importing "site" module, which is importing "os" which is importing "ntpath" -- before my dir is added to sys.path.  So later when I import os, it and ntpath have already been imported, so Python doesn't attempt a fresh import.

To get my custom ntpath.py honored, need to RELOAD, like:
  import os
  import ntpath
  reload(ntpath)
  print 'os.walk(\'goo\') with isdir override in custom ntpath'
  for root, dirs, files in os.walk('goo'):
      print root, dirs, files

where the diff betw standard ntpath.py and my ntpath.py are:
  14c14,19
  < from genericpath import *
  ---
  > from genericpath import *
  >
  > def isdir(s):
  >     return genericpath.isdir('\\\\?\\' + abspath(s + '\\'))
  > def isfile(s):
  >     return genericpath.isfile('\\\\?\\' + abspath(s + '\\'))

I'm not sure how I could have known that ntpath was already imported, since *I* didn't import it, but that was the key to my confusion.

Thanks again for the help.
John

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

how to get os.py to use an ./ntpath.py instead of Lib/ntpath.py ruck <john.ruckstuhl@gmail.com> - 2012-09-10 10:25 -0700
  Re: how to get os.py to use an ./ntpath.py instead of Lib/ntpath.py Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-10 20:16 +0000
    Re: how to get os.py to use an ./ntpath.py instead of Lib/ntpath.py ruck <john.ruckstuhl@gmail.com> - 2012-09-10 15:22 -0700
      Re: how to get os.py to use an ./ntpath.py instead of Lib/ntpath.py Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-11 03:46 +0000
        Re: how to get os.py to use an ./ntpath.py instead of Lib/ntpath.py Tim Golden <mail@timgolden.me.uk> - 2012-09-11 08:20 +0100
          Re: how to get os.py to use an ./ntpath.py instead of Lib/ntpath.py ruck <john.ruckstuhl@gmail.com> - 2012-09-11 12:13 -0700
            Re: how to get os.py to use an ./ntpath.py instead of Lib/ntpath.py Chris Angelico <rosuav@gmail.com> - 2012-09-12 08:50 +1000
            Re: how to get os.py to use an ./ntpath.py instead of Lib/ntpath.py Dave Angel <d@davea.name> - 2012-09-11 18:57 -0400
          Re: how to get os.py to use an ./ntpath.py instead of Lib/ntpath.py ruck <john.ruckstuhl@gmail.com> - 2012-09-11 12:13 -0700
        Re: how to get os.py to use an ./ntpath.py instead of Lib/ntpath.py Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-12 08:17 +0200
          Re: how to get os.py to use an ./ntpath.py instead of Lib/ntpath.py aahz@pythoncraft.com (Aahz) - 2012-11-09 16:42 -0800

csiph-web