Python 3 Programming Tutorial – Read from a file

Now that we know how to write and append to files, we might want to learn how to read data from files into the Python program. Doing this is quite simple, and has very similar syntax.

Sample code for this basics series:

Python 3 Programming tutorial Playlist:

Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6


23 thoughts on “Python 3 Programming Tutorial – Read from a file”

  1. Excellent video! Question: How would I open two files to read? I know it can be done using a for loop but I am not sure how?
    for text_file_name in ['named1.txt','MalcolmX.txt']:
        read = open('named1.txt', 'MalcolmX.txt' 'r').read()
    print (read)

  2. Hi Harrison , I am doing a controlled assessment for the end of high school. I am making a login system in python using tkinter(i know most of the basics) but I mainly need to know how I would read a txt file using an if statement to find a specific piece of information e.g. password. Please could you advise me on how I would go about doing this.

  3. Hi dear friend sentdex you are great master,congratulations your videos are the best, sorry my english its no the best,.

  4. when i run this progam:
    readMe = open('text.txt', 'r').read()
    print readMe
    i get back an empty string.
    how is that possible

  5. Hi thanks for the tutorials. For a .CSV file (for a spreadsheet) should I edit it using this read write method or are there modules for interacting with spreadsheets and other files?

  6. How can you directly open a txt file from Python
    For example opening a notepad document with data already stored in it so the user can see the txt file directly from notepad
    Is there any way to do this if so please help quickly

  7. How do you delete a text you've appended?
    Like I want to make a file where I can Add and Delete lines of texts. How can I do that? @sentdex

  8. FYI – you don't always have to ask to "read" the file you opened. It's actually faster to iterate over a file object that is not "read".

    l = open('file','r')
    for line in l:
    pass (or code)

    is much faster than

    l = open('file','r')
    for line in / l.readlines()
    pass (or code)

    the 2nd one will be 1.5x more expansive time-wise.

  9. How do I read a file from a whole another directory? It's not taking the path (enclosed in quotes of course).

  10. Really impressed with the consistent high quality of your tutorials. I don't even bother searching YouTube for Python tutorials now, I just go straight to your channel. Keep it up!

  11. Hi, you may not have the time to answer but ill ask it anyways.

    Im wondering, if i want to save data (f.e. a list) for later use, should I save it to a .txt and then read it back when needed? Or are there easier other ways?


  12. hahahahahaahahahaahahahahahahahahahahahahahahhahaahahahahahahahahahahahahahahhahahahahahahhahahahahahahahahahhaahhahahahahahahahahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhahaahahahahahahahahahahahahahahahahahahahahahahahahahahaahahahahhahahhahhahahahahahahahahahahahahahahahhahahahahaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahahahhahahahahahahahahhahahahahhahahahhsahahahah

Leave a Reply