Search code examples
pythoncoding-stylenaming-conventions

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


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 ...


Solution

  • 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.