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
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:
Yep, it's official. "Implicit File Path Resolution"
I need to author a PyWart on this soon!
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.
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.
This one's arguable. How often do you use the 'file'
builtin? I've worked with files in Python innumerable
times, and I don't remember the last time I used 'file'.
Yes, avoid shadowing important builtins like 'list' and
'int', but 'id' and 'file' aren't that big a deal.
My first concern is with the OP adopting "self documenting"
names. Shadowing file is merely ensuring that the OP knows
"file" is a builtin. Shadowing any builtin can create very
difficult bugs to track. This is the point the OP should
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
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!
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.
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.
PS: I thought Steven was the official "devils advocate"
around here? Hmm. I guess everyone needs a day off.