Python Programming Tutorial - 23 - How to Read and Write Files

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

i almost discontinued your tutorials. Im so grateful you enlarged your font. I hope the other tutorials utilize the larger size. Thanks for all the help. Now if I use a regular text editor and throw it in theater mode I can watch and code at the same time.

morgansippel
Автор

This seriously one of the best tutorial finds ever! Thank you so much!!!

librasaur
Автор

I have watched a couple of tuts, but it's hard understand this exact open, write, read and close....
You can make the learning incredibly easy. You got a special talent, man! Thanks a bunch!
Cheers from Northeastern Brazil!

AntonioSantos-vezv
Автор

This really is the greatest tutorials on the fricking planet.

jasonspeziale
Автор

Seriously why should I bother with Java when I have Python. It's so freaking amazing.

usqgi
Автор

Using all of my knowledge that I have gathered so far(and a little bit of discovery), I have made this simple guessing game in about an hour:
import random
import time

num = random.randrange(1, 10)


def game():
    choice = input("\nGuess a number between 1 and 100 ")
    int(choice)
    if choice is num:
        print("Checking...")
        time.sleep(1)
        print("You win!")
        print("The number was:", str(num))
    else:
        print("\nChecking...")
        time.sleep(1)
    if num < int(choice):

            lose()
    elif num > int(choice):
            lose()
    else:
        print("\nYou Win!")
        play_again()


def lose():
    print("\nYou lose")
    play_again()


def play_again():
    choice = input("\nPlay again? Y/N ")
    if choice is "Y":
        print("Yay!")
        game()
    elif choice is "N":
        print("\nOkay then...")


game()

proccessingunit
Автор

The only thing that keep this tutorial from perfect is the font and now it is the complete perfect python tutorial on earth. Thanks a lot.

feiprofessor
Автор

I have started you tutorials from start and these tutorials are amazing. and I have learned so many things thank you the new Boston

lolk
Автор

Since you're using camtasia studio to record, use their zoom tool to zoom into the video

ompmega
Автор

Thanks for showing the \n in the strings. I have now made a save writer - here is the code (I import stuff I don't always need in case I need them).

import sys
import random
import os
import time
import fileinput

Save = 1

with open("nextSave.txt", "r") as f:
saves = f.readline()

Data1 = 42
Data2 = 21
saveString = "Save #"+str(saves)+".txt"
savesString = str(int(saves)+1)
WritelineOne = str(Data1)+"\n"
WritelineTwo = str(Data2)

if(Save == 1):
print("Packaging...")
with open(saveString, "w") as f:

with open("nextSave.txt", "w") as g:
g.write(savesString)
print("Save Successfull")


The "nextSave" file is a text file with the next save number inside it. the data variables can be changed, and correspond to the lines. you can add more by changing data2 from "WritelineTwo = str(Data2)" to "WritelineTwo = str(Data2)+"\n", " add a third variable called Data3, then WritelineThree = str(data3) and the should be this goes into a text document.

sorry if the explanation was too long.

noway
Автор

u can do:
fw.write (".LOG\n")
and then write stuff..
the .LOG makes the txt file registers the date of the last change to the file

DaCurse
Автор

Used some of your last tutorial to do this.

import random
name = random.randrange(1, 1000)
full_name = "sample" + str(name) + ".txt"
fw = open(full_name, 'w')
fw.write("hello this is just a test \n")
fw.write("more of the test, to write a simple program, so all can enjoy.")
fw.close()

fr = open(full_name, 'r')
text = fr.read()
print(text)
fr.close()

camb
Автор

Can you also create a file.py? So you can let your programm write it's own programm

cas
Автор

Is there 720p ..
Or is it YouTube fault for not showing
720p
Only 480p is available

justrandom
Автор

Guy, you are the best :) Thanks a lot for these short lessons!

AndreyChezhin
Автор

When you wrote the .read part, you already had sample.txt with your text written on it, . Why didn't it write the text under it when you ran it? Does it replace completely the text you wrote before?

Eichro
Автор

Thank you so very much Bucky, u really are making life easy for us young developers.
once again, thank you buddy, oh bucky

syedmohdsohail
Автор

When you executed the code with read block in it why didn't the text was again written in the file(Sample.txt) although the write block was also executed??

vasusabarwalVasu
Автор

when you re run the write code does it keep adding those two lines in the sample.txt ! like in first run - lines 1, 2 and again when you run the same now lines 3, 4 and keep adding when re ran ? @Bucky

sandeepakula
Автор

Your videos are really awsm I had seen many videos on python thoug they are nice but to be honest boring but urs seems interesting thanks so much

GeekySatyam