Python: variable naming convention – file, path, filepath, file_path

Question:

I would like to know the correct naming convention for the following variables in Python which I couldn’t find one from Google Style Guide and PEP8

(Let’s say I have the following Python code)

output_file = open(output_file_path, ‘w’)

What would be the best variable name for the out file name?

I believe the possible options for the variable name would be something like

output_file outputfile outfile out_file outfile

And the path variable can be something like

output_file_path output_filepath output_path out_path

Asked By: Jiho Choi

||

Answers:

According to PEP8 you should use _ between each meaningful words for variable names, Similarly we use capital case for class names.
by searching about the word filepath I should say that there is not such a word in English, it means that it is not a single word, it contains two separate word(file, path), so it is correct to use file_path instead of ‘filepath’, although both of them is being used by developers these days.

About the part that contains output word, According two Zen Of Python we already knew that:

Readability counts.

and

Explicit is better than implicit.

I should say that is pretty much better to use output before your variable name.

So I think output_file_path and ‍output_file are the correct and best choices here.

Answered By: Mehrdad Pedramfar

Citing from https://english.stackexchange.com/a/428231/289175 :

Both forms, filepath and file path, are used, but which one is used is often dependent on context. […] filepath, as an unhyphenated compound word, is generally used when discussing it as an entity (e.g., “You’ll need to set the filepath before writing out any data.”), but file path, as two words, is generally used when referring to a particular attribute of a file.

Answered By: gebbissimo