Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 494
Jiho Choi Avatar asked Apr 19 '18 08:04

Jiho Choi


People also ask

What is the naming convention for variables in Python?

Rules for Python variables: A variable name must start with a letter or the underscore character. A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

Is Filepath one word or two?

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.

How do I get the directory of a file in Python?

In order to obtain the Current Working Directory in Python, use the os. getcwd() method. This function of the Python OS module returns the string containing the absolute path to the current working directory.


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

like image 110
Mehrdad Pedramfar Avatar answered Sep 19 '22 20:09

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.

like image 35
gebbissimo Avatar answered Sep 19 '22 20:09

gebbissimo