Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are underscores better than hyphens for file names?

Tags:

python

naming

From Building Skills in Python:

A file name like exercise_1.py is better than the name execise-1.py. We can run both programs equally well from the command line, but the name with the hyphen limits our ability to write larger and more sophisticated programs.

Why is this?

like image 555
atp Avatar asked Apr 29 '10 19:04

atp


People also ask

Should you use hyphens or underscores in file names?

Guidelines for names Make file and directory names lowercase. Use hyphens, not underscores, to separate words—for example, query-data. html . Use only standard ASCII alphanumeric characters in file and directory names.

Why do people still use underscores in file names?

Removing the space or underscore reduces the length of the file name but by using capital letters to differentiate between the words, the file name is still readily recognisable.

Should I use underscores in file names?

Don't start or end your filename with a space, period, hyphen, or underline. Keep your filenames to a reasonable length and be sure they are under 31 characters. Most operating systems are case sensitive; always use lowercase. Avoid using spaces and underscores; use a hyphen instead.

Should I use underscore or hyphen?

Underscores are only able to be used in subfolders. Hyphens can be used in subdomains, subfolders, and domain names (although we don't recommend using hyphens in domain names, as listed above). Keep the use of underscores strictly to subfolders.


2 Answers

The issue here is that importing files with the hyphen-minus (the default keyboard key -; U+002D) in their name doesn't work since it represents minus signs in Python. So, if you had your own module you wanted to import, it shouldn't have a hyphen in its name:

>>> import test-1   File "<stdin>", line 1     import test-1                ^ SyntaxError: invalid syntax >>> import test_1 >>> 

Larger programs tend to be logically separated into many different modules, hence the quote

the name with the hyphen limits our ability to write larger and more sophisticated programs.

like image 161
Daniel G Avatar answered Nov 10 '22 13:11

Daniel G


From that very document (p.368, Section 30.2 'Module Definition'):

Note that a module name must be a valid Python name... A module's name is limited to letters, digits and "_"s.

like image 26
ire_and_curses Avatar answered Nov 10 '22 11:11

ire_and_curses