Creating a function for a directory

Chris Angelico rosuav at gmail.com
Tue Nov 12 01:02:39 EST 2013


On Tue, Nov 12, 2013 at 4:42 PM, Rick Johnson
<rantingrickjohnson at gmail.com> wrote:
> On Monday, November 11, 2013 5:11:52 PM UTC-6, Chris Angelico wrote:
>> On Tue, Nov 12, 2013 at 9:51 AM, Rick Johnson
>> > 1. i believe win32 file paths require a qualifying volume
>> > letter.
>> They do not; omitting the drive letter makes the path relative to the
>> current drive (and since it doesn't start with a directory specifier,
>> the current directory).
>
> Hmm. Let's confirm:
>
>     >>> import os
>     >>> os.path.exists("C:/Windows/System32")
>     True
>     >>> os.path.exists("/Windows/System32")
>     True
>
> Yep, it's official. "Implicit File Path Resolution"
> I need to author a PyWart on this soon!

That's not a Python issue at all. And are you seriously going to decry
relative pathnames?!?

>> > 2. Never, ever, *EVER* write data to disc before confirming
>> > the paths your passing are pointing to the location you
>> > intended to write the data. Use os.path.exists(path) to test
>> > your paths BEFORE trying to write data.
>> Why? Why, oh why? If there's a problem, it'll be signalled
>> with an exception.
>
> Except when there's a problem that won't be signaled by an
> EXCEPTION, but will be signaled by EMOTIONS; like for
> instance removing the wrong directory or truncating the
> wrong file because of a typo in your source code.

Which os.path.exists will not protect you from. So?

>> Testing that the path exists opens you up to race
>> problems, so you won't see anything now, but some day your
>> code will be in a concurrent situation and you'll get
>> unexpected exceptions. Why not just expect the exception?
>
> Because today i'm not facing a concurrent situation, so i'm
> not going to bother and protect from it. Just like i'm not
> facing a snake bite, so i won't bother to lug around a vial
> of antidote. Your attempts to discredit me via hypothetical
> scenarios is entertaining however.
>> > 6 For OS compatibility always use os.path.join() to join
>> > path parts into a whole. This method will insert the proper
>> > separator for you depending on the OS.
>> Technically true. However, most modern OSes will accept a
>> slash. There'll be a few situations where that's not true,
>> but the OP an happily just use slashes for simplicity.
>
> Many a developer have lived to regret those words.

(Reordered to bring together two comments getting one response) Do you
care about what you currently are seeing, or what you might some day
see? Concurrency is a common problem on many platforms; platforms not
supporting slashes between paths are rare, and most of them will have
other porting issues. Why are you ignoring the likely, while trying to
prevent the unlikely?

>> They won't float around forever. The garbage collector
>> will get to them.
>
> That depends on how they are stored. You make too many
> assumptions Chris. I don't have an problems with GC's, but
> i'm not about to write sloppy code and "assume" the GC is
> going to swoop in and save me like a feline trapped in a
> tree.

Ah, actually yes you are. When have you ever explicitly disposed of an
object in Python? You just let the GC deal with it for you.

>> > 5. Remember, you cannot write a file into a directory that
>> > does not exist.
>> So? Exception thrown, traceback printed to console,
>> process terminated cleanly. I'm not seeing a problem here.
>
> Now you're just trolling!

Mercurial signals some errors by dumping an exception to stderr. This
works quite nicely, even in production. If your script can't recover
from being given a non-writeable directory, don't catch the exception.

>     def firstdev(file):
>         in_file = open("desktop/%s.txt") % file
>         indata = in_file.read()
>         out_file = open("desktop/newfolder/%s.txt", 'w') % file
>
> Just from reading that code NO ONE could know for sure if
> "newfolder" even existed BEFORE the OP tried to open the
> "out_file". Maybe the OP thinks that missing sub-directories
> are auto-created by the open function, but there's no way to
> know for sure, hence my comment.

And the OP might have thought that it would automatically publish that
to the world, tweet the URL, and create a Facebook status saying "just
opened a file lolol" and get five people to Like it. Doesn't mean we
need to probe before doing things.

ChrisA



More information about the Python-list mailing list