Skip to content

Spirit Yatra

  • Home
  • Entertainment
  • Finance
  • School
  • Journey
  • Product
  • Crypto-Currency

How to do File Import in Python Notes

July 11, 2020 by Stilgar

My notes on how to do File Import in Python notes taken during my Python Dev Bootcamp.

Python File import

  • .read()
    • Returns everything in the text file
    • Once everything has been returned, the cursor position is at the bottom. As such, if you re-run  read(), it will not return anything as it is already at the end of the file.
    • You need to reset the cursor’s position to read the documents from the beginning
  • .seek(0)
    • Resets the cursor’s position to the beginning
  • .readlines()
    • Displays texts in the file in their own lines i.e. its own separate object as a list
    • Advantages:
      • As it is displayed as a list
      • You can loop through the list
      • You can use it as an index
  • Python File Import Best Practices
    • If you open a file, you should close the file
      • .close()
      • OR (better option)
      • with open(‘myfile2.txt’) as my_new_file:
      •     contents = my_new_file.read()
  • Write/Overwrite Files During File Import
    • Mode
      • R
        • Read only
      • W
        • Write only (will overwrite files or create new)
      • A
        • Will append only
      • R+
        • Reading and write
      • W+
        • Overwrites existing files or creates a new file

The above examples are pretty basic and cover notes from m Python Bootcamp lessons and are not comprehensive. As I skill-up, I will add additional notes for my personal references as a diary.

Future Notes on Python Development

I have also covered notes from other topics during my dev bootcamp and should be accessible from the Programming section. Notes include, use of Lists, Dictionaries, String Formatting for Printing with Python. Finally, you can also look at the start of my journey into python with Learn to Code in Python article. I am going to talk about Tuples, Sets, Booleans, For Loops & While Loops, Python Inheritance, and Comparison Operators (IF ELSE) in my upcoming posts. In addition to the Python notes, I will start taking javascript classes as well and hope to document my journey and post learning javascript notes.

Post navigation

Previous Post:

Will Learn Javascript Starting This Week

Next Post:

How I Went Through a Javascript 1-Hr Tutorial on Youtube

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • My Recent Experience With Trading 0-Day Options February 21, 2025
  • Leveraging AI Tools to Start Working on New Projects February 18, 2025
  • How I Went Through a Javascript 1-Hr Tutorial on Youtube July 12, 2020
  • How to do File Import in Python Notes July 11, 2020
  • Will Learn Javascript Starting This Week July 11, 2020

About Spirit Yatra

This site chronicles my life journey – my challenges, joys, daily mundane activities and observations.

© 2025 Spirit Yatra | Built using WordPress and SuperbThemes