ไม่สามารถเล่นวิดีโอนี้
ขออภัยในความไม่สะดวก
How to Write to a text .txt file in Python! Processing Lists, and Outputting Data!
ฝัง
- เผยแพร่เมื่อ 29 ธ.ค. 2021
- This video discusses the method for writing data from python into a text file. This includes step by step instructions for accessing the file from the same folder as the python program, selecting what info you want to store in the file and writing it in. We then go one step further and take info stored in python list variables, modify it and create a text file output with the newly created data!
Learn to read from files here:
• How to Read from a tex...
Check out my personal channel for more fun science content!
/ @peterlemaster8310
If you have any questions or comments or want to see something specific in the future let us know about it with a comment!
We have hundreds of hours of instructional content on the channel and create more every week so be sure to subscribe to stay up to speed! Thanks for checking out the video and good luck with your projects!
/ @lemastertech
thank u sooooo much for the tutorial it helped so much. I was stuck at one point and after watching this I was finally able to understand what was going wrong. Thank you again very much.
You are so welcome I’m glad it was so useful for you!! Thanks for watching :)
Thanks Boss, u just saved my life 😺
Haha you're very welcome my friend! Thanks for watching!
Thanks so much, you helped me complete my computing assignment :)
I’m so glad it helped you, thanks for watching!!
Guys, for those who dont want to overwrite the old text with new text add a + '
' to the and use 'a' instead of 'w' .write so it'll look something like this
file = open('file.txt', 'a')
file.write('some info', '
')
Thanks for the useful comment! Hopefully others find this too if they have the same question :)
Thank you so much for the help. I needed this info for my uni assignment
You’re the best!
Hey you are very welcome! Thanks for watching!!
This was super easy to understand and really helpful! Thanks loads :)
You are super welcome!! Glad it was useful for you my friend :)
Do you by any chance have a video on appending information at the end of text files? I recall seeing one and want to rewatch it but don't remember on which channel.
Hey, I think this video will give you what you need! th-cam.com/video/_N8_BhcGHxU/w-d-xo.html I also have a beat maker/drum kit project where I do that at the end of the video too! I hope this helps!
thank you, that actully helped alot while i was trying to get a list form made
Glad it helped you out!! You are welcome!
Great video. Explained very well
Glad you liked it, thanks for watching!!
Thank you!! it helped much🙂
You are welcome!! So glad you found it useful!!
I copied everything you did exactly and nothing happens. No errors, the file doesn't open, and nothing gets written.
Interesting - it might be getting written/saved somewhere else on your laptop. What IDE are you using??
@@lemastertech visual studio code
Thanks a lot :) Can u make more python videos pls 🤗 it was really helpfull and easy to understand
Thanks for the nice comment!! I make a lot of python videos including some full games like PacMan!! I also like to mix the content up and cover lots of other stuff, hope you like it all! :)
How would you alter that final code so that the file would be read from the middle to the end?
You can just read in the text file, check total length or total number of lines and then only work with the second half?
you explained things better than my lecturer. What about writing to a txt file with password attempts and time and date?
Hey thank you, and that’s an interesting idea! You could certainly do that in the same read/write fashion as we have here, but you’d need to read in the data of the file, compare your entries to it, and decide what to do in your code based on differences between the read in data from the text file and the entered data!
how do you just check for the last number in the list and then continue the prgram from there? (numbers are 0, 1, 2, ...)
Hi - if you need the last item in the list you can use negative numbers to count backwards from the end of the list! So my_list[-1] will return the last item in a list!
awesome, thanks!
You’re welcome!! Glad it worked for you!
Many thanks
You’re welcome thanks for watching!!
Just tried running my code but it didn't work. I checked and made sure my syntax was correct but yet my text file still came out blank
Hm, it doesn’t throw any errors but your text doesn’t write to the file? Double check you have file.close() at the end, that would be very surprising!
Hey! your video was useful, but i have a little problem and you said it in the video, when i restart the code, the txt file is empty, hopefully you can fix this.
Hi - make sure after you do your file.write you add file.close() to save the changes. I have a better update version of this lesson here th-cam.com/video/_N8_BhcGHxU/w-d-xo.html where I read and write at the same time!
ok but i put the file.close thing and nothing changed@@lemastertech
I have a question, how to print the highest grade and names of students, in decending orders, owh also great video i really needed this for my work, this helps a lot.
Hi, just sort the list using whatever key you want. A list of strings will default sort to alphabetical, but if you have a list of dictionaries or something like that you need a more advanced sorting mechanism by sorting using keys! Goodluck and thanks for watching!
thank you that helped so much!! :D@@lemastertech
How to take input and then write the input data in that file?
Hi if you just want to take user input from the terminal window you can just do
input_data = input(“enter data to save”)
And then save input data to the file using the steps here! If you want something more advanced you might want to check out the end of my beatmaker/drum kit project where I save and load complicated user data!
@@lemastertech Thanks, But I am new at Python😅. I saw this video because I want to make a simple sign in and sign up database.
What about create a new file instead of edit an existing one? Is it possible?
Hello! Yes if you actually do the exact code shown in this video and the file doesn't already exist this will make it for you! So you don't already have to have the file existing - if you do file = open('filename', 'w') and that filename doesn't exist yet it will make automatically!
@@lemastertech Thanks very much!!
@@rowcezarbrazil you got it!
Sir I have a question if possible please help me-
Write a program that reads all the contents from a file and prints the first, second and last line
Hi! Yes it’s possible for sure! Just read the text in the same way this tutorial is doing it and then take your list of lines and make a new list that just has line[0] line[1] and line[-1] from your list of all the text!!
@@lemastertech Thanks a lot sir. If I face doubt again i will again ask you sir
@@gautammazumdar5643 good luck!
After inputting it into the text file, is there a way to make a function to edit or update the grades only?
So what you would have to do is open the file (I did a partner video to this one on how to read from a text file as well), and store all the data in a list variable, then you can manipulate individual values which you could separate with commas or give each their own new line from your python code using functions then make sure to write it back into the text file to get it to save after being manipulated!
@@lemastertech Thank you! Ill see if it works. I really appreciate your quick reply haha im doing an assignment. Also i was wondering if I would need indexing for the editing, if i were to edit a specific bit of the list
@@lemastertech We encountered a problem where there was an error that says String and integer cannot be concatenated, as our list contains String and integers. We just want to change the integer
@@jrnastyy hey so you can do a str() conversion if you need to turn an integer into a string, but if you’re trying to concatenate everything needs to be a string you won’t be able to display integers with strings!! You can always convert to a string using str() then convert back to a number using int() or float() once you have the data parsed!
@@lemastertech thank you so much for your help!!!
Is it possible to add text while running the program?? I mean can the user add text in file? Is there any way out?
Hello - yes you can you just need to make sure your python program does file.close() when you want the program to stop owning the file and return it to the computer!
@@lemastertech Thank you!
@@adithyamadhu2309 You're welcome, thank you for watching!
How would you write your terminal output to text file?
You mean like run the program, print a bunch of stuff to the terminal and then also to a text file? I would just make a string or list of strings every time I print() anything and append that output to that list as well then save my list to the text file!
But how do we not overwrite?
Use A instead of W when you open the text file! That will append to the existing content rather than overwrite it!
@@lemastertech thank you man
goood !
Thank you! Thanks for watching!
Salvou demais! o/
espero que tenham gostado do vídeo
How can we write those in each new line
Like
Name mark
Peter 70
Hi, you can use the newline character
to go on to the next row. So you can start by writing your column headers then
and each entry separated by that character too. You could also use a csv instead of a text file and separate values with commas!
This doesn't work dude, my python still wants to be saved as .py
It does work dude, what IDE do you use?
I use thonny@@lemastertech
And how to create a file?
You want to know how to create a new txt file from inside a python program??
@@lemastertech Yes.
Lemasters tech i need your help
Whats up?
@@lemastertech check my dm
Guys, for those who dont want to overwrite the old text with new text add a + '
' to the and use 'a' instead of 'w' .write so it'll look something like this
file = open('file.txt', 'a')
file.write('some info', '
')
Thanks for adding this!