When executing the same command on different platforms, the shelve module generates file (s) with different extensions. If you need more detail:

s = shelve.open("test", writeback=True) 

When executing such a command, the following result is obtained:

  1. On linux-base systems, one file is generated called "test"
  2. On MacOS, one file is generated called "test.db"
  3. On Windows, three files are generated, each with the names "test.dir", "test.dat" and "test.bak"

This fact saddens and greatly interferes with the work of the code using the reading of the file generated by the shelve module on different platforms.

In search of an answer to the question: "Yes, how is that?!", I came across this page from the English language stackoverflow. As I understand it (and alas, my knowledge of English is not so great), the problem is that the shelve module uses the ( based on ) module anydbm, which, in turn, is a software interface using system database implementations, such as dbhash , gdbm , dbm and dumbdbm . Further, on the same page it is indicated that the implementation of the database, which generates three files in each, is dumbdbm.

From all that I was able to translate on this page, I suggested that the problem is incompatibility of database implementations on different systems. I tried installing gdbm on windows from here . I also made an attempt to add a folder containing binaries with gdbm to PATH, but this is not enough to allow the shelve on windows to read the shelve files that are generated in the linux-base system.

In general, the question is: how to correct this misunderstanding?

UPD: The question is old, but still open. Add: the task then was to serialize together with the object its methods and transfer this file to another OS.

  • If I am mistaken somewhere in the reasoning, please correct it - Semior
  • one
    And the problem is what? Saved with the help of the shelve - with his help open. - m9_psy
  • if it's so important to read something else, determine the platform, extensions you already know. And so it is not clear why you do not read through shelve.open, if the database is not correct, it will not open it for reading / writing. - Igor
  • You misunderstood me. If I generated the database on linux and tried using shelve.open to open it on Windows, it will not work. Python swears. The same, if you generate on Windows and try to open in linux-base - Semior

1 answer 1

Unfortunately, the files generated by this module are not cross-platform, so the short answer to your question is no fix.

I don’t know what your task is, but if that’s what it is to serialize python data structures and write them to a file, then consider some other modules that suit you. For example, json or pickle . You can also consider the use of any database, for example, sqlite . Or sqlite in conjunction with pickle . All of these options are cross-platform.

  • Using the shelve is similar to using dictionaries. The task is as follows: the key is the user ID, the value is an instance of the custom class. - Semior
  • @Semior via pickle.dumps should work. The base is also screwed without problems. Consider this option, I think it will work - FeroxTL