endolith
endolith

Reputation: 26843

What variable name do you use for file descriptors?

A pretty silly trivial question. The canonical example is f = open('filename'), but

What else do you use?

Upvotes: 17

Views: 7313

Answers (6)

Aziz
Aziz

Reputation: 20765

 data_file
 settings_file
 results_file
 .... etc

Upvotes: 8

Glenjamin
Glenjamin

Reputation: 7360

Generally if the scope of a file object is only a few lines, f is perfectly readable - the variable name for the filename in the open call is probably descriptive enough. otherwise something_file is probably a good idea.

Upvotes: 3

uolot
uolot

Reputation: 1460

I rather use one of: f, fp, fd.

Sometimes inf / outf for input and output file.

Upvotes: 0

Alex Martelli
Alex Martelli

Reputation: 882701

I'm happy to use f (for either a function OR a file;-) if that identifier's scope is constrained to a pretty small compass (such as with open('zap') as f: would normally portend, say). In general, identifiers with large lexical scopes should be longer and more explicit, ones with lexically small/short scopes/lifespans can be shorter and less explicit, and this applies to open file object just about as much as to any other kind of object!-)

Upvotes: 4

Unknown
Unknown

Reputation: 46821

You can append it to the beginning, Hungarian-like "file_fft".

However, I would try to close file descriptors as soon as possible, and I recommend using the with statement like this so you don't have to worry about closing it, and it makes it easier to not lose track of it.

with open("x.txt") as f:
    data = f.read()
    do something with data

Upvotes: 6

timdev
timdev

Reputation: 62924

generally I'll use "fp" for a short-lifetime file pointer.

for a longer-lived descriptor, I'll be more descriptive. "fpDebugLog", for example.

Upvotes: 2

Related Questions