Python 3 Programming Tutorial - Writing to File

preview_player
Показать описание
Welcome to another Python 3 basics tutorial. In this tutorial we're going to cover the basics of writing to a file.

It should be noted that there are two methods for saving data to a file, and those are writing and appending. Writing to a file will write that bit of data, whatever it is, solely, to the file. This means if there was anything there before, it will be gone if you use write.

If you use append, then you will basically add to whatever is previously there. I will be showing both methods, but write first.

Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Рекомендации по теме
Комментарии
Автор

I suggest to always use the 'a' for append instead of the 'w'
If the file doesn't exist yet it will create one even if the 'a' is used, so there is never the risk of overwriting stuff.
Like this:

text = 'Sample Text to Save\nNew Line!'

saveFile = open('exampleFile.txt', 'a')
saveFile.write(text)
saveFile.close()

authoritycamper
Автор

So much clearer and simplified than my college course video. Night and day... College is trash :(

megusta
Автор

Guys you could also do

Savefile = open("example.txt", "w")
Savefile.write("it will right this in the text file")
Savefile.close()

If you don't close it won't save the things you wrote in the text file meaning it will be empty when you open it

pfjvfln
Автор

How dow we print to txt from VS studio?

TheRangeControl
Автор

So is there any way to delete last char? (Backspace)

tuguldurodnasan
Автор

How to write to a pdf file?
This code is not working :
text = "Sentdex"
SaveFile = open('example.pdf, 'w')
SaveFile.write(text)
SaveFile.close()

shivanshsoni
Автор

For some reason after i write the saveFile.write(text) line it comes up the number 29 why is this

theradinion
Автор

Hi sentdex, this is very simple and easy to understand for beginners. I've a question that how to access a file in desktop ? Thanks in advance :)

AnandRajanAnandRajan
Автор

Could you switch to 16:9? 4:3 makes my head spin...

somedude
Автор

@sentdex i tied to save the file on Desktop, but i am getting an error . Can you tell me where i am making mistake ?
Traceback (most recent call last):
File "C:\Users\TS\AppData\Local\Programs\Python\Python38\Test1.py", line 3, in <module>
saveFile = open('Desktop/newfile.txt', 'w')
FileNotFoundError: [Errno 2] No such file or directory: 'Desktop/newfile.txt'

dhavalshah
Автор

hello, just a stupid question perharps.. I know the proper way to do that is by storing functions into variables but, why we need to do so when we can do all that code on one line only.. I mean: open("newfile.txt", "w").write("hello there").close() I tried and it seems to work (it creates the newfiles.txt with text in it) but there's an error.. AttributeError: 'int' object has no attribute 'close'. Just curious about that, I want to understand why we need to store functions into variables. cheers

FluttuantChicken
Автор

Hi sentdex
what could be the fastest way to write to a file. 
I keep on concatenating data to a string  until it reaches 100 MB then I want to write it to a file.
Thanks in Advance

ahmadmaroof
Автор

How do I get python to find the file? IM using a Mac and im getting this error message. "NameError: name 'appendfile' is not defined" ALSO is possible to pay for premium service to where i get my questions answered faster? :)

Rubenjr
Автор

I though about looping it so it creates 100 files but failed ;_;

jakubgajdul
Автор

This might sound stupid but how did you get it to open in notepad? I did everything right .

Chegmeister
Автор

i got something stupid

File "simswap.py", line 12
saveFile.close()
^
SyntaxError: invalid syntax

betasecurity
Автор

Hi sentdex, i have a problem, i made like a bit of code where user input should be appended to a file, after the user filled in its data the program will also print the users input. But for somereason the thing appends the names of my variables (that are the input) instead of the input itself. Here'smycode:

Armament = input ('choose your Armament: ')
FuelLoad = input ('How much fuel do you want? ')
Radar = input ('Which radar do you want? ')
Pilot = input ('What is your name? ')
Software = input ('Which boardComputer Software do you want? ')

def CustomFunc(Armament, FuelLoad, Radar, Pilot, Software):
    print (Armament, FuelLoad, Radar, Pilot, Software)
    DataInput = ("Armament, FuelLoad, Radar, Pilot, Software")
    OrderFile = open('Order.txt', 'a')
    OrderFile.write(DataInput)

CustomFunc(Armament, FuelLoad, Radar, Pilot, Software)

(In case your wondering, the info is about the desired fighterjet in my game i want to build)

Thanks in advance!!

EvdeKsp
Автор

Could you expand these tutorials and show how to create a GUI with python. Because that would be really helpful.

jasonhogan
Автор

Ran this but could not find the opened file...

text = 'my name is Isaac'

saveFile = open('Isaac.txt', 'w')
saveFile.write(text)
saveFile.close()

isaacthani
Автор

I can' run the write to file command. It shows permission error. What should I do?

angelchadha