What variable name do you use for file descriptors?

Pretty silly trivial question. Canonical example f = open('filename') , but

  • f not very descriptive. Despite the code, you can forget whether this means “file” or “function f (x)” or “fourier” to convert the results “or something else. EIBTI.
  • In Python, a file already used by a function.

What else do you use?

+6
python explicit file naming-conventions variable-names
source share
6 answers
  data_file settings_file results_file .... etc 
+4
source share

You can add it to the beginning, the Hungarian "file_fft".

However, I will try to close the file descriptors as soon as possible, and I recommend using the with statement so that you don’t have to worry about closing it, and it’s simplified so as not to lose it.

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

I am glad to use f (for the OR function of a file ;-) if the identifier area is limited by a rather small compass (for example, with open('zap') as f: will usually indicate, say). In general, identifiers with large lexical areas should be longer and more explicit, those that have lexically small / short areas / lifespan may be shorter and less explicit, and this applies to an open file object about as much as any other kind of object! -)

+3
source share

In general, I will use "fp" for a file pointer with a short lifetime.

for a longer-lived descriptor, I will be more visual. For example, "fpDebugLog".

+2
source share

In general, if the scope of a file object is only a few lines, f perfectly readable - the variable name for the file name in an open call is probably fairly arbitrary. otherwise something_file is probably a good idea.

+2
source share

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

Sometimes inf / outf for input and output file.

0
source share

All Articles