2

I often store data in external files when Python coding.

Should I always save them with an extension - i.e. .txt?

Is there any reason not to (other than saving bytes) to not give the extension? I know it will only be my code reading this file (there would be no reason for someone to open it).

Tim
  • 202
  • 1
  • 12
  • 7
    Will you be transferring these files between computers? Do the files contain text? Is the text encoded somehow? Is it raw binary data? There is no way to answer this question as written, as it boils down to programmer opinion. –  May 30 '15 at 21:37
  • 1
    What operating system are you using? – user1717828 May 30 '15 at 21:45
  • Are these stored data similar to configuration data? A standard way for doing this is to create a directory for your files in an appropriate place in the running user's home directory, e.g. `~/.myprog`. Once you have created this directory, you can store the persistent data of your program there using whatever naming convention is suitable for you. – Brandin May 30 '15 at 23:18
  • @user1717828 Programming on Linux, written to work on all 3. – Tim May 31 '15 at 12:08
  • @Snowman 1. No 2. Yes 3. No 4. No – Tim May 31 '15 at 12:09
  • @Brandin good idea. The issue is the multiple OSes, so it writes to the same place the file is. – Tim May 31 '15 at 12:09

1 Answers1

8

I will answer this question for people in general out there, but I should first point out that even for you in particular, there is probably no such thing as "only my code will be reading this file". There will be times when you will need to view or edit the file, no? So then, besides your own code, your favorite editor will also be reading that file, at the very least.

If you are only going to be using command-line tools, (and you don't care if your choices essentially sentence yourself to always be using nothing but command-line tools,) then extensionless files should be fine. That's because command-line tools follow the verb-noun paradigm of user interface design, according to which you first choose the verb, (you type the name of the command to execute,) and then you choose the noun on which the verb will act (you type the file name as a command-line argument to the command.) So, essentially, the computer is not burdened with having to keep track of the type of each file, and instead this task is delegated to the computer's trusty servant, the human.

If you are going to be using some graphical user interface OS invented within the last, oh, 40 years or so, which in all likelihood follows the noun-verb paradigm instead, then you will most probably need filename extensions. You see, the noun-verb paradigm allows you to first select one or more files, and then perform either the default action on them, (double click usually edits,) or choose an action from a list of actions (context menu) that are known by the operating system to be applicable to files of that type. Of course, in order for this to work, the Operating System must know the type of each file. The problem is that for some unfortunate as much as unfathomable reason, certain operating systems (i.e. Windows) tend to have no formal mechanism for knowing the type of each file, so they rely on hacks like the filename extension for guessing file types. So, if you would like to take advantage of the fact that your operating system can save you from extra clicks and extra keystrokes by knowing the type of each file, then filename extensions are your friend.

Other than that, no, there is no compulsory technical reason for always giving each filename an extension. It is just a very useful thing to have.

Mike Nakis
  • 32,003
  • 7
  • 76
  • 111