I am doing simple files exercises with Python (3.8 in Linux Mint 20, Ubuntu 20.04 based, Pycharm) and when I run this code the output is the expected but with new line inserted between each lines. Maybe it is simple to solve but, any help?
import os.path
def read_table():
x = int(input("Enter a number between 1 and 10: "))
if os.path.exists("tabla-{}.txt".format(x)):
file = open("tabla-{}.txt".format(x), "r")
for line in file.readlines():
print (line)
else:
print("The file doesn't exist")
def main():
read_table()
if __name__ == "__main__":
main()
for line in file:is almost always better than usingfor line in file.readlines():; the latter slurps the whole file into memory as alistof lines, requiring peak memory proportionate to the size of the whole file, and delaying processing until the entire file is read; the former lazily reads on-demand, requiring a roughly fixed amount of memory while allowing processing to begin while giving the OS time to cache the rest of the file (making it possibly faster, as well as requiring less memory).