Due to the will of Allah the Prophet of Islam never received an education, never read a book and never wrote one, and it is clear why Allah wanted the prophet to remain like that. The prophet's everlasting miracle was a book named the Quran and bringing a book for one who was educated did not seem to be a miracle, or at least it would cause doubts and buttress the oft-mentioned false claim that the Quran was a result of the prophet's thoughts and the knowledge he gained from his teachers. We do not have any books from Imams other than Imam Ali and Imam Sajjad due to the great responsibility of guiding the society (Imamah), living in certain periods of history and having students that would write the ahadith.
The abovementioned question can be answered from different perspectives:
1- The Prophet of Islam: The prophet lived in a very special period of time, due to the will of Allah the Prophet of Islam never received an education, never read a book and never wrote one, and it is clear why Allah wanted the prophet to remain uneducated. The prophet's everlasting miracle was a book named the Quran and bringing a book for one who was educated did not seem to be a miracle, or at least it would cause doubts and buttress the oft-mentioned false claim that the Quran was a result of the prophet's thoughts and the knowledge he had gained from his teachers. This is why Allah says in the Quran: "You did not use to recite any scripture before it (the Quran), nor did you write it with your right hand, for then the impugners would have been skeptical (about your prophetic claims)." 
Therefore, although reading and writing are both considered vital abilities that are very valuable, but sometimes in certain situations, being uneducated can be a great advantage. In the case of the Prophet, being uneducated was a contributing factor to proving his prophetic claims. Imagine if the prophet was scientist prominent scholar or philosopher and then brought the Quran, there would be doubts about whether he had brought the book from himself or it had been divine revelation. Even after bringing the Quran the prophet never wrote anything. Maybe because his foes would try to undermine people's belief in the Quran and that it had come from a holy source,  but as we know the prophet received all divine knowledge from Allah and was not taught by any one else. 
2- Why didn't the Imams write books?
It is not true that none of the Imams wrote books, in fact Imam Ali, the father of all Imams, wrote a book that was known among his children as the "Book of Ali".  We also have a book from the fourth Imam named the "Sahifah Sajjadyyah". However today we no longer have access to the "Book of Ali".
In regard to Quranic commentaries, the first person to write a Quranic commentary was Imam Ali. When he compiled the Quran he added the Sha’n Nuzul (occasion of revelation) and interpretation of some of the verses. Unfortunately that Quran was not accepted by the Khalifahs of that time and by the people because of certain things going on in those days in the Muslim society. 
Other than what was mentioned we have not received any books from the Imams, and the reason behind this can be one of the following:
2-1 The Responsibilities of Imamah and guiding the Muslim Society
The burdens of Imamah and guiding the Muslim Ummah are so heavy that writing hadith books can not be compared with them. Besides this, the Imam is not a writer or author, instead he must socially and spiritually guide the people. In other words he is responsible for training and guiding righteous individuals that can later on sacrifice their lives for the sake of Islam. Because an Imam must take care of all spiritual and social affairs of people it may seem that writing books were also among the responsibilities of the Imam, but we must keep in mind that the Imam which is busy taking care of the most important needs of the society would only write books if it was necessary and they had enough time. It seems that such necessity and opportunity was only found at the time of Imam Ali and Imam Sajjad.
2-2 Special circumstances of that time
There are two points that makes the period of the Imams a special one, first was the people's ignorance towards the Ahlul-Bayt because of the circumstances the rulers that had taken away the right of the Ahlul-Bayt had caused. It was due to this ignorance that the Ahlul-bayt were put aside and were not given a notable chance to take the social matters of the Muslim Ummah the way the prophet wanted into their own hands. Although this ignorance would not decrease the status of the household of the prophet in the eyes of Allah and would only entail difficulties for the people, but writing books required the willing of society to accept the books and such a thing was missing at that time.
Second was the situation the Ahlul-Bayt were dealing with and that they had no choice but to do Taqiyyah (dissimulation). Not only were the governments very sensitive towards the letters the Imams would write, but writing books would definitely cause the Ahlul-Bayt serious problems if they wrote the truth in them. Even the books of the Imam’s students and followers would clearly arrest the attention of the governments. For example one of Imam Jawad's companions came to him and said: "Our teachers have narrated ahadith from Imam Baqir and Imam Sadiq, but because of Taqiyyah they have hidden their books and have not given them to the people, what should we do with these books now?" the Imam replied: "There are no problematic issues in those books and you can spread them amongst the people." 
Therefore one of the main reasons why the Imams could not even write letters, let alone books, was the circumstances that forced them to do Taqiyyah. The Umayyid and Abbasid dynasties would strictly scrutinize and spy on the imams to keep their every move under control . Although some of the Ahlul-Bayt lived in less strict periods of time and other lived in more harsh and strict environments but all of them were forced to practice Taqiyyah in some way. This is why Imam Baqir says: "Tagiyyah is the religion (the way of living) of me and my fathers." 
2-3 Students and Transcribers of the Ahadith
Every Imam had a group of companions that were responsible for writing the ahadith and sayings of the Imam, they were known as the "Muhaddethun". There was no need for the Imams themselves to write ahadith books. One might say that if the Imams had written books of their own there would be no discrepancies in these hadith records, but this is not true, because the same way the books of the Imam’s followers were distorted, the books of the Imams would have also been altered by their adversaries. In fact the foes of the household of the prophet would have focused much more on corrupting their books if they had actually written any, and in that case the results would have been catastrophic. It would have caused great misguidance, because the people and the Muhaddethoon would have expected the books of the Ahlul-Bayt to be authentic and therefore would have not distinguished between the right and wrong ahadith mentioned in them.
Finally we need to point out that the abovementioned claim saying that there are discrepancies in hadith books is not true. There are some discrepancies at first sight, but these so called contradicting hadiths have been reconciled by Muslim scholars. Over the course of centuries the ahadith have been checked several times by the companions of the Imams and the Imams themselves in order to separate false hadiths from true ones. Also, Islamic scholars have had endeavors in this field, trying their best to distinguish between the two.
For more information you can refer to Question 1937 (website: 2185) Distinguishing Authentic Hadiths.
 Motahhari, Morteza, Payambare Ummi, pg. 6.
 Makarem Shirazi, Naser, Tafsir Nemouneh, vol. 16, pg. 308.
 Koleini, Muhammad ibn Ya’qub, Kafi, vol. 1, pg. 41.
 Kafi , vol. 1, pg. 53.
 Kafi , vol. 2, pg. 219.