pyton read text file

28

with open ("data.txt", "r") as myfile:
    data = myfile.read().splitlines()
# read and print all contents of a file
with open('data.txt', 'r') as f:
   contents = f.read()
   print(contents)

# loop through file line by line removing newlines
with open('data.txt', 'r') as f:
   for line in f:
       print(line.rstrip())

# read first 14 bytes of file
with open('data.txt', 'r') as f:
   print(f.read(14))
   print(f"The current file position is {f.tell()}")

   # move to beginning of file
   f.seek(0, 0)

   # print 30 bytes from position
   print(f.read(30))
with open('the-zen-of-python.txt') as f:
    for line in f:
        print(line)
Code language: JavaScript (javascript)

Comments

Submit
0 Comments