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


Groups > comp.lang.python > #107570

Re: Remove directory tree without following symlinks

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From eryk sun <eryksun@gmail.com>
Newsgroups comp.lang.python
Subject Re: Remove directory tree without following symlinks
Date Sun, 24 Apr 2016 14:42:29 -0500
Lines 57
Message-ID <mailman.57.1461526992.32212.python-list@python.org> (permalink)
References <571a3ba2$0$1597$c3e8da3$5496439d@news.astraweb.com> <1461337766.365000.586700849.0DDBDB0B@webmail.messagingengine.com> <mailman.16.1461337768.2861.python-list@python.org> <571a5be6$0$1590$c3e8da3$5496439d@news.astraweb.com> <DUB123-W22A605538041FB2749C64B836F0@phx.gbl> <CACL+1aueH_scSTYW1Q6pGsZ92+8OWHB6tks3SqAAaU-wb6Ac4Q@mail.gmail.com> <DUB123-W362E7C64F347B92A74E34B83600@phx.gbl> <CACL+1avhu9YcM5qbFaAdhq9ZUoizP5Dc8p99LMrCcREmcxBNWw@mail.gmail.com> <DUB123-W25926A459EE112FA2741FA83610@phx.gbl> <CACL+1auZBa6q-+_=bbx-H6J7bpsXdYJ2dNx7mRpfeRqo_jru1Q@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de gVe/iiRVhe0QMPFUr+ptlQbHzmj4kNWNB3xrjxyUL2Bw==
Return-Path <eryksun@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'api.': 0.04; 'string.': 0.04; 'needed,': 0.05; 'paths': 0.05; 'url:pipermail': 0.05; "'a'": 0.07; 'dynamically': 0.07; 'incompatible': 0.07; 'prefix': 0.07; 'shutil': 0.07; 'wrapper': 0.07; 'api': 0.09; 'buffer,': 0.09; 'decodes': 0.09; 'filename,': 0.09; 'os.path': 0.09; 'typeerror:': 0.09; 'example:': 0.10; 'python': 0.10; 'syntax': 0.13; '2016': 0.16; '24,': 0.16; '[1].': 0.16; 'already,': 0.16; 'api,': 0.16; 'appends': 0.16; 'function).': 0.16; 'prefix,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Remove': 0.16; 'wrote:': 0.16; 'byte': 0.18; '(in': 0.18; '>>>': 0.20; 'windows': 0.20; '"",': 0.22; 'am,': 0.23; '(or': 0.23; 'help.': 0.23; 'seems': 0.23; 'forgot': 0.23; '(most': 0.24; 'implemented': 0.24; 'header:In-Reply-To:1': 0.24; 'discussion': 0.24; 'message-id:@mail.gmail.com': 0.27; 'allocated': 0.27; 'function': 0.28; 'outlook': 0.28; 'ansi': 0.29; 'sentence': 0.29; 'str': 0.29; 'handled': 0.29; 'branch': 0.30; 'skip:[ 10': 0.31; 'implement': 0.32; '[1]': 0.32; 'generally': 0.32; 'possibly': 0.32; 'returned': 0.32; 'useful': 0.33; 'url:python': 0.33; 'common': 0.33; 'usually': 0.33; 'doubt': 0.33; 'though.': 0.33; 'traceback': 0.33; "skip:' 20": 0.34; 'file': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'files,': 0.35; 'label': 0.35; 'path': 0.35; 'protocol': 0.35; 'but': 0.36; 'needed': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'volume': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:209.85.213': 0.37; "won't": 0.38; 'received:209': 0.38; 'skip:o 20': 0.38; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'believe': 0.66; 'forward': 0.66; 'subject': 0.70; '2.7.': 0.84; '[error': 0.84; 'subject:tree': 0.84; 'hand,': 0.97
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=SVr0PmuBBEaxdTqxz9KcHCz7YzqAg5bE8ocsDQ7v3Pg=; b=KtOGNIqQ6Q9+lAbgmynA2AMaKUBN7Fe5xtHFIAMoSGvDKzZignT/7EDb6Qj+TboqJc I2SLOtv6Dmhls8HfRd3h+a8xxY8jjSoXB6zgAu4dHPEmmtiCQmGmc6Z/+rkRoIwcLEMa KCnw45xUDlqLLXR2nX/EkU78jlsGdHQbfl867yv+kGJUuzahGdeXEr4EmcKrAHO46RWL c1jNqYkT6HkyVLOwlWeaFaiJDRNFW1+tRjaHw5BC7z1j/TvYTw3E+rj8PxAjOKBkIc40 g6Td4/06fpfpyyVX7kDRLPqlXGgh/h76JxGKIkWgQuAgpRh8Lpeu33B/2ou+bfe5tkw7 zTuQ==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=SVr0PmuBBEaxdTqxz9KcHCz7YzqAg5bE8ocsDQ7v3Pg=; b=HfD1v79ZLVTr2d63OUpg1b/IRypHPfSsAEj669NwcHEHymS1XmYbsWN9OXqYSv7RYn rVhokAKMGh/r6jd6qoOnMrRgzj5TwyOipDKg3kGsoY01w8W8+E6O2K2KSFk2bzHaGZr7 9cKS7Rm+c0ujt0g+Whcc07T9DizGOg9kAqs5By54L2EUTwL4p/3cnEpi8e61GKAav1HI Il0yedhjYC+v5FYgmhHDUTqqeuLtElT1IAcYVwe5yueQKxvVxBBGjRF69xDgZaqLMRhO GChUhP5Y6u3Py70mNOt/eeiy3l7Jtx54rxs8UzN2rPJGhmW1AR4QN+MTwzVZOoxNGfYi C7bw==
X-Gm-Message-State AOPr4FVDNm3N0q3Ou8nlSiRviC12Da57Ap6jMVRM0bE3Sh+bBJFaTVR32c2CtTK6Eqioe6e2wzrhFia6Lo81Jw==
X-Received by 10.50.13.74 with SMTP id f10mr8609821igc.55.1461526988986; Sun, 24 Apr 2016 12:43:08 -0700 (PDT)
In-Reply-To <DUB123-W25926A459EE112FA2741FA83610@phx.gbl>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <CACL+1auZBa6q-+_=bbx-H6J7bpsXdYJ2dNx7mRpfeRqo_jru1Q@mail.gmail.com>
X-Mailman-Original-References <571a3ba2$0$1597$c3e8da3$5496439d@news.astraweb.com> <1461337766.365000.586700849.0DDBDB0B@webmail.messagingengine.com> <mailman.16.1461337768.2861.python-list@python.org> <571a5be6$0$1590$c3e8da3$5496439d@news.astraweb.com> <DUB123-W22A605538041FB2749C64B836F0@phx.gbl> <CACL+1aueH_scSTYW1Q6pGsZ92+8OWHB6tks3SqAAaU-wb6Ac4Q@mail.gmail.com> <DUB123-W362E7C64F347B92A74E34B83600@phx.gbl> <CACL+1avhu9YcM5qbFaAdhq9ZUoizP5Dc8p99LMrCcREmcxBNWw@mail.gmail.com> <DUB123-W25926A459EE112FA2741FA83610@phx.gbl>
Xref csiph.com comp.lang.python:107570

Show key headers only | View raw


On Sun, Apr 24, 2016 at 5:42 AM, Albert-Jan Roskam
<sjeik_appie@hotmail.com> wrote:
> Aww, I kinda forgot about that already, but I came across this last
> year [1]. Apparently, shutil.rmtree(very_long_path) failed under Win 7,
> even with the "silly prefix". I believe very_long_path was a
> Python2-str.
> [1]
> https://mail.python.org/pipermail/python-list/2015-June/693156.html

Python 2's str branch of the os functions gets implemented on Windows
using the [A]NSI API, such as FindFirstFileA and FindNextFileA to
implement listdir(). Generally the ANSI API is a light wrapper around
the [W]ide-character API. It simply decodes byte strings to UTF-16 and
calls the wide-character function (or a common internal function).

IIRC, in Windows 7, byte strings are decoded using a per-thread buffer
with size MAX_PATH (260), so prefixing the path with "\\?\" won't
help. You have to use the wide-character API. Windows 10, on the other
hand, decodes using a dynamically allocated buffer, so you can usually
get away with using a long byte string. But not with Python 2
os.listdir(), which uses a stack-allocated MAX_PATH+5 buffer in the
str branch. For example:

Python 2 os.mkdir works:

    >>> path = os.path.normpath('//?/C:/Temp/long/' + 'a' * 255)
    >>> os.makedirs(path)

but os.listdir requires unicode:

    >>> os.listdir(path)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: must be (buffer overflow), not str
    >>> os.listdir(path.decode('mbcs'))
    []

Also, the str branch of listdir appends "/*.*", with a forward slash,
so it's incompatible with the "\\?\" prefix, even for short paths:

    >>> os.listdir(r'\\?\C:\Temp')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    WindowsError: [Error 123] The filename, directory name, or volume
    label syntax is incorrect: '\\\\?\\C:\\Temp/*.*'

> It seems useful if shutil or os.path would automatically prefix paths
> with "\\?\". It is rarely really needed, though. (in my case it was
> needed to copy a bunch of MS Outlook .msg files, which automatically
> get the subject line as the filename, and perhaps the first sentence
> of the mail of the mail has no subject).

I doubt a change like that would get backported to 2.7. Recently there
was a lengthy discussion about adding an __fspath__ protocol to Python
3. Possibly this can be automatically handled in the __fspath__
implementation of pathlib.WindowsPath and the DirEntry type returned
by os.scandir.

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


Thread

Remove directory tree without following symlinks Steven D'Aprano <steve@pearwood.info> - 2016-04-23 00:56 +1000
  Re: Remove directory tree without following symlinks Random832 <random832@fastmail.com> - 2016-04-22 11:09 -0400
    Re: Remove directory tree without following symlinks Steven D'Aprano <steve@pearwood.info> - 2016-04-23 03:14 +1000
      RE: Remove directory tree without following symlinks Albert-Jan Roskam <sjeik_appie@hotmail.com> - 2016-04-22 17:39 +0000
      Re: Remove directory tree without following symlinks eryk sun <eryksun@gmail.com> - 2016-04-22 13:28 -0500
      RE: Remove directory tree without following symlinks Albert-Jan Roskam <sjeik_appie@hotmail.com> - 2016-04-23 09:34 +0000
      Re: Remove directory tree without following symlinks eryk sun <eryksun@gmail.com> - 2016-04-23 15:22 -0500
      Re: Remove directory tree without following symlinks eryk sun <eryksun@gmail.com> - 2016-04-24 14:42 -0500
  Re: Remove directory tree without following symlinks Paul Rubin <no.email@nospam.invalid> - 2016-04-23 01:13 -0700
    Re: Remove directory tree without following symlinks Steven D'Aprano <steve@pearwood.info> - 2016-04-23 20:24 +1000
      Re: Remove directory tree without following symlinks Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-04-23 23:37 +1200
      Re: Remove directory tree without following symlinks Random832 <random832@fastmail.com> - 2016-04-23 17:04 -0400
  Re: Remove directory tree without following symlinks Nobody <nobody@nowhere.invalid> - 2016-04-23 17:29 +0100
    Re: Remove directory tree without following symlinks Random832 <random832@fastmail.com> - 2016-04-23 17:07 -0400

csiph-web