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


Groups > comp.lang.python > #107849

Re: Why does pathlib not have is_readable() & things like that?

From Adam Funk <a24061@ducksburg.com>
Newsgroups comp.lang.python
Subject Re: Why does pathlib not have is_readable() & things like that?
Date 2016-04-29 10:49 +0100
Organization $CABAL
Message-ID <manbvcxjkl.ln2@news.ducksburg.com> (permalink)
References (1 earlier) <1461679044.2467938.590028273.4DEA02AA@webmail.messagingengine.com> <mailman.106.1461679046.32212.python-list@python.org> <r8e9vcxf2a.ln2@news.ducksburg.com> <nft3h7$pbt$1@ger.gmane.org> <mailman.188.1461850479.32212.python-list@python.org>

Show all headers | View raw


On 2016-04-28, Grant Edwards wrote:

> On 2016-04-28, Adam Funk <a24061@ducksburg.com> wrote:
>> On 2016-04-26, Random832 wrote:
>>
>>> On Tue, Apr 26, 2016, at 09:30, Adam Funk wrote:
>>>> I recently discovered pathlib in the Python 3 standard library, & find
>>>> it very useful, but I'm a bit surprised that it doesn't offer things
>>>> like is_readable() and is_writable.  Is there a good reason for that?
>>>
>>> Well, one reason would be EAFP. Just try to open the file and see if it
>>> gives you a PermissionError.
>>
>> I understand that in general, but the tool I'm working on here takes a
>> command-line option to specify an output directory, & I'd rather not
>> start processing the data (which involves GETting from a REST service,
>> processing, and PUTting back modifications to the data) only to crash
>> after the first batch because of a user error.
>
> Then open the output file before you do the GET.

I guess I could, but fetching the data actually involves a whole lot
of GET requests (the first one includes cross-references to the URLs
where the rest of the data is found), some BeautifulSoup processing, &
a lot of other processing to produce a big dict, which I then write
out as json using what I think is the best way (output_file is an
instance of pathlib.Path):

    with output_file.open(mode='w', encoding='UTF-8', errors='replace')  as f:
        json.dump(output, f, sort_keys=True, indent=2)

> Or just do os.access("directory/where/you/want/to/open/a/file",os.W_OK)

That's what I'm doing now, but I prefer to give the user the error
message early on.

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


Thread

Why does pathlib not have is_readable() & things like that? Adam Funk <a24061@ducksburg.com> - 2016-04-26 14:30 +0100
  Re: Why does pathlib not have is_readable() & things like that? Random832 <random832@fastmail.com> - 2016-04-26 09:57 -0400
    Re: Why does pathlib not have is_readable() & things like that? Adam Funk <a24061@ducksburg.com> - 2016-04-28 14:02 +0100
      Re: Why does pathlib not have is_readable() & things like that? Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-04-28 16:24 +0300
      Re: Why does pathlib not have is_readable() & things like that? Grant Edwards <grant.b.edwards@gmail.com> - 2016-04-28 13:34 +0000
        Re: Why does pathlib not have is_readable() & things like that? Adam Funk <a24061@ducksburg.com> - 2016-04-29 10:49 +0100
          Re: Why does pathlib not have is_readable() & things like that? Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-04-29 14:51 +0300
            Re: Why does pathlib not have is_readable() & things like that? eryk sun <eryksun@gmail.com> - 2016-04-29 10:09 -0500
            Re: Why does pathlib not have is_readable() & things like that? Grant Edwards <grant.b.edwards@gmail.com> - 2016-04-29 23:53 +0000
  Re: Why does pathlib not have is_readable() & things like that? Steven D'Aprano <steve@pearwood.info> - 2016-04-27 01:46 +1000
    Re: Why does pathlib not have is_readable() & things like that? Adam Funk <a24061@ducksburg.com> - 2016-04-28 13:57 +0100

csiph-web