Learn to Program 8 : Reading / Writing Files

[ad_1]
Code & Transcript :

Best Python Book :

Interactive Tutorial with Quiz Here :

Support me On Patreon :

This time we’ll cover how to read and write files. I’ll answer a question I received about recursive functions. We’ll investigate what a Tuple is. And, of course we’ll work our way through problems so we can get better at solving programming problems.

Thank you to Patreon supports like the following for helping me make this video

@kyleaisho
@thetwistedhat
vjFaLk


Posted

in

by

Tags:

Comments

23 responses to “Learn to Program 8 : Reading / Writing Files”

  1. Aditya D Avatar

    This is what I did for the average words problem:
    import os
    num = []

    with open("data.txt", encoding="utf-8") as file:

    lineNum = 1
    while True:
    line = file.readline()
    num.append(len(line))

    if not line:
    break
    else:
    continue

    print(num)
    print(sum(num)/len(num))

  2. Aakash Lall Avatar

    num = int(input('Enter number: '))
    x = [0,1]

    for i in range(2,num):
    x.append(x[i-1] + x[i-2])
    for k in x:
    print(k,',',end='')

    another way to do the fibonacci without functions

  3. Paavan Gupta Avatar

    sir, i not understood what is exactly that "encoding = "utf-8" " used for.
    plz reply

  4. Chung David Avatar

    3 questions/problems i have:
    1.at 3:29 he says that readline() will read everything into one string. Then what happens at 12:57? he used myFile.readline() to read line by line.

    2. So the read() or readline() function automatically switches to the next line when it is runned one more time. It doesnt need an incrementing variable that will tell it to skip the first line and read the next one.

    3. So the read line doesnt create an error when it reaches the end of the .txt file that you told it to read.

  5. Fallacy Avatar

    Alternative solution for counting the lines and average characters per word:

    while True:
    line = myfile.readline()

    word = line.split()

    newstring = ""

    if not line:
    break

    for i in word:

    newstring += i

    average = len(newstring) / len(line.split())

    print("The number of words in line {} is {} ".format(linenum, len(line.split())))

    print("The average lenght of words in line {} is {} charactersn".format(linenum, round(average, 0)))

    linenum += 1

  6. Nick Virdee Avatar

    How would I print out the last 10 lines of a file?

  7. Balla Baby Avatar

    at the 7 min mark there is the command os.chdir("..") . How does this command move the directory back one. If i were to put 3 periods in the quotes besides 2 would I move back another directory? Thanks

  8. Prashant Mishra Avatar

    Hey Derek, in my python version 2.7.10, when I use ( encoding = "utf-8") inside open(), the error is saying that (invalid syntax "encoding"). But when I remove the encoding = "utf-8" the program is running smoothly. So is encoding not required in Python 2 or something ?

  9. Diego Luiz Avatar

    Hi Derek! First of all thanks for the videos! They are awesome!
    At time 10:25 you gave an example how to print a list of Fibonacci numbers. The question for user entry is the number to show on screen, but in your example the Fibonacci number printed was less than the number required by the user. You typed 20 and you showed 19 values. In this case while loop must be less or equal to numFibValues.

  10. nedErlands Avatar

    only thing i hate about python is that you cant use counter++, and instead you haveto use counter+=1 :s

  11. Kristoff Lutchman Avatar

    Hi Derek, again, thank you so much for making these videos. While following your video, I noticed in this tutorial, in the very beginning where you declared "encoding='utf-8'" as an argument in the open function, I had an error "TypeError: 'encoding' is an invalid keyword argument for this function". I fixed it simply by removing the argument altogether and was able to continue on but my question is, does python 2.7 not need that declaration? I started with 2.7 and haven't switched to 3 and am using PyCharm community edition. I tried searching the internets for the answer but had no luck finding it.

  12. Uttaran Wary Avatar

    +Derek Banas How to print the second line without printing the 1st line from the .txt file?

  13. veganath Avatar

    Derek will or have you done a video on how to use PyCharm contextual HELP as when u were typing the line with open("..etc and other commands it would be useful for newbies to have access to the syntax and command structure and options. It seems one has to learn syntax quite thoroughly in order to not make typo mistakes.

  14. Xpack Gunners Avatar

    well i'm learning python and it seems pretty easy so far , i don't know if it's still gonna be easy as the tutorials go on or it's kinda gonna get complicated .
    you're doing a great work keep it up

  15. Calata Avatar

    I'd love a full C++ & QT tutorial, it would be awesome. Btw i love this kind of tutorials

  16. tailow Avatar

    Wouldn't the first problem be easier with a for loop?
    Something like this:

    numFibValues = int(input("How many Fibonacci values should be found: "))

    for i in range(numFibValues):
    print(fib(i))

    print("All done")

  17. Göktuğ Aşcı Avatar

    How to take down the same line of code with those cute curvy arrows?

  18. Tom Nook Avatar

    Woah how do you make it so that in the editor when you type too much it will make another line?

  19. praneta mahawar Avatar

    I just wanted to know what does return 1 do in python ?

  20. Sujal Yadav Avatar

    Dear Derek, I'm having a problem, please help me out.

    My text file includes just like this:
    1
    2

    The program is:

    with open("add.txt", encoding = "utf-8") as myFile:
    lineNum = 1
    while True:
    line = myFile.readline()

    if not line:
    break

    numbers.append(line)

    lineNum += 1

    print('addition of {} and {}'.format(numbers[0], numbers[1]))

    Output:
    addition of 1
    and 2

    I want everything in one line.What to do? I have also tries readlines(). Didn't work.

  21. Got Change? Avatar

    I see how you can read and write basic txt and CSV files, but can you create other types of files with python? Like can you make a google KMZ file if you have a lat/long? I'm curious if you're limited on file types.

    Thanks, love the vids.

  22. ThePassingVoid Avatar

    Your loop at 10:24 only looped 19 times, you will need to change the expression in the while loop to "while i <= numFibValues:" so that the 20th loop will happen, since 20 < 20 is false and 20 <= 20 is true. The 20th number should be 6765

  23. Anthony sanchez Avatar

    hi derek, im having trouble running an application. i have to connect the log in to mysql data base and from the data base the application gets username, password, ect. i was wondring if you can help me out set up my data base. im , im currently using xammp my sql and im only confused on where i have to set the users. on rows and rows. is there any way i can contact you so i can send you the script?

Leave a Reply

Your email address will not be published. Required fields are marked *