Skip to content
Unknown's avatar

Programming Additions

Ricardo Miranda

  • Home
  • Contact

Tag: File

Read a file in Python

To read a file in Python do:

with open(“testfile.txt”) as file:  
  contents = file.read().replace("\n", " ")
  print contents

To read a file line by line in Python do:

with open(“testfile.txt”) as file:  
  contents = file.readlines() 
  for line in contents:
    print line

Ricardo Miranda Uncategorized Leave a comment June 19, 2019June 19, 2019
Blog at WordPress.com.
  • Subscribe Subscribed
    • Programming Additions
    • Already have a WordPress.com account? Log in now.
    • Programming Additions
    • Subscribe Subscribed
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar