My textbook says that in perfect competition the condition of free entry and exit only applies to the long run equilibrium. Because in the short run no new firms can enter or old ones can leave the industry. Can someone tell me the reason for that?
Because by definition of short-run it is not possible. In economics, short-run is defined as a period when (some) factors/variables are fixed and not flexible. Consequently, by definition firm cannot exit or enter in the short-run as it cannot change it's fixed costs - for example firm prepaid rent and can't get the money back, or it takes few days to rent out new office (see Mankiw Principles of Economics Ch 14).
If the number of firms changes then by definition within the standard model of perfect competition we already arrived in the long-run, as that means that now fixed cost became variable (firms can build new or sell old factories, offices etc). So this is purely definitional, it is like asking in biology why do only mammals drink milk when young (well we just defined the category that way).
Moreover, note short-run or long-run have no set time span. For a hotdog vendors short run might be time less than few days and log-run time more than few days (e.g. hotdog vendor might be able to expand or shut down their business within few days or by getting more hotdog carts and employees).
I would recommend you to not take that sentence from the textbook literally. This is because in that scenario, when the textbook says in the short-run firms can't enter or leave, it is implying that in the long-run, there is enough time for firms to enter and leave. The reason firms need to enter and leave is because of costs and profits. If firms from the outside see that firms in a specific industry are making profits, those firms will want to join in on those profits. The same goes the other way: if firms in the industry see that they are making a loss, they will want to leave.