Тёмный

How to Write to a text .txt file in Python! Processing Lists, and Outputting Data! 

LeMaster Tech
Подписаться 16 тыс.
Просмотров 64 тыс.
50% 1

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

Опубликовано:

 

15 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 87   
@kanicabansal937
@kanicabansal937 Год назад
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.
@lemastertech
@lemastertech Год назад
You are so welcome I’m glad it was so useful for you!! Thanks for watching :)
@ahmedtamer5604
@ahmedtamer5604 6 месяцев назад
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', ' ')
@lemastertech
@lemastertech 6 месяцев назад
Thanks for the useful comment! Hopefully others find this too if they have the same question :)
@senile8668
@senile8668 6 месяцев назад
Thank you so much for the help. I needed this info for my uni assignment You’re the best!
@lemastertech
@lemastertech 6 месяцев назад
Hey you are very welcome! Thanks for watching!!
@mythorium8143
@mythorium8143 Год назад
Thanks so much, you helped me complete my computing assignment :)
@lemastertech
@lemastertech Год назад
I’m so glad it helped you, thanks for watching!!
@gnostie
@gnostie Год назад
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.
@lemastertech
@lemastertech Год назад
Hey, I think this video will give you what you need! ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-_N8_BhcGHxU.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!
@yasr5931
@yasr5931 Год назад
This was super easy to understand and really helpful! Thanks loads :)
@lemastertech
@lemastertech Год назад
You are super welcome!! Glad it was useful for you my friend :)
@goldenfox334
@goldenfox334 2 года назад
thank you, that actully helped alot while i was trying to get a list form made
@lemastertech
@lemastertech 2 года назад
Glad it helped you out!! You are welcome!
@Dj-ij6nf
@Dj-ij6nf Год назад
Thanks Boss, u just saved my life 😺
@lemastertech
@lemastertech Год назад
Haha you're very welcome my friend! Thanks for watching!
@nathanblackburn5055
@nathanblackburn5055 Год назад
Great video. Explained very well
@lemastertech
@lemastertech Год назад
Glad you liked it, thanks for watching!!
@NSKP_
@NSKP_ 2 года назад
Thank you!! it helped much🙂
@lemastertech
@lemastertech 2 года назад
You are welcome!! So glad you found it useful!!
@tarikselim499
@tarikselim499 Год назад
Thanks a lot :) Can u make more python videos pls 🤗 it was really helpfull and easy to understand
@lemastertech
@lemastertech Год назад
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! :)
@fazdefaz
@fazdefaz Год назад
you explained things better than my lecturer. What about writing to a txt file with password attempts and time and date?
@lemastertech
@lemastertech Год назад
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!
@kirillzakharov7336
@kirillzakharov7336 4 месяца назад
How would you alter that final code so that the file would be read from the middle to the end?
@lemastertech
@lemastertech 4 месяца назад
You can just read in the text file, check total length or total number of lines and then only work with the second half?
@xX_NameHere_Xx
@xX_NameHere_Xx Год назад
how do you just check for the last number in the list and then continue the prgram from there? (numbers are 0, 1, 2, ...)
@lemastertech
@lemastertech Год назад
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!
@rowcezarbrazil
@rowcezarbrazil Год назад
What about create a new file instead of edit an existing one? Is it possible?
@lemastertech
@lemastertech Год назад
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!
@rowcezarbrazil
@rowcezarbrazil Год назад
@@lemastertech Thanks very much!!
@lemastertech
@lemastertech Год назад
@@rowcezarbrazil you got it!
@3pm-on-yt
@3pm-on-yt Год назад
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.
@lemastertech
@lemastertech Год назад
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 ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-_N8_BhcGHxU.html where I read and write at the same time!
@3pm-on-yt
@3pm-on-yt Год назад
ok but i put the file.close thing and nothing changed@@lemastertech
@gautammazumdar5643
@gautammazumdar5643 2 года назад
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
@lemastertech
@lemastertech 2 года назад
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!!
@gautammazumdar5643
@gautammazumdar5643 2 года назад
@@lemastertech Thanks a lot sir. If I face doubt again i will again ask you sir
@lemastertech
@lemastertech 2 года назад
@@gautammazumdar5643 good luck!
@NoongarCulture
@NoongarCulture Год назад
awesome, thanks!
@lemastertech
@lemastertech Год назад
You’re welcome!! Glad it worked for you!
@haziqkepli1608
@haziqkepli1608 Год назад
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.
@lemastertech
@lemastertech Год назад
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!
@haziqkepli1608
@haziqkepli1608 Год назад
thank you that helped so much!! :D@@lemastertech
@FPSManInTheWall
@FPSManInTheWall Год назад
Many thanks
@lemastertech
@lemastertech Год назад
You’re welcome thanks for watching!!
@jrnastyy
@jrnastyy 2 года назад
After inputting it into the text file, is there a way to make a function to edit or update the grades only?
@lemastertech
@lemastertech 2 года назад
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!
@jrnastyy
@jrnastyy 2 года назад
@@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
@jrnastyy
@jrnastyy 2 года назад
@@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
@lemastertech
@lemastertech 2 года назад
@@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!
@jrnastyy
@jrnastyy 2 года назад
@@lemastertech thank you so much for your help!!!
@ufuomaomo-enwefah8129
@ufuomaomo-enwefah8129 11 месяцев назад
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
@lemastertech
@lemastertech 11 месяцев назад
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!
@adithyamadhu2309
@adithyamadhu2309 Год назад
Is it possible to add text while running the program?? I mean can the user add text in file? Is there any way out?
@lemastertech
@lemastertech Год назад
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!
@adithyamadhu2309
@adithyamadhu2309 Год назад
@@lemastertech Thank you!
@lemastertech
@lemastertech Год назад
@@adithyamadhu2309 You're welcome, thank you for watching!
@audio_masterenghinsongs4898
@audio_masterenghinsongs4898 6 месяцев назад
How to take input and then write the input data in that file?
@lemastertech
@lemastertech 6 месяцев назад
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!
@audio_masterenghinsongs4898
@audio_masterenghinsongs4898 6 месяцев назад
@@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.
@Spugler2
@Spugler2 9 месяцев назад
I copied everything you did exactly and nothing happens. No errors, the file doesn't open, and nothing gets written.
@lemastertech
@lemastertech 9 месяцев назад
Interesting - it might be getting written/saved somewhere else on your laptop. What IDE are you using??
@Spugler2
@Spugler2 9 месяцев назад
@@lemastertech visual studio code
@robertcristian274
@robertcristian274 Год назад
goood !
@lemastertech
@lemastertech Год назад
Thank you! Thanks for watching!
@victorcandiani
@victorcandiani 2 года назад
Salvou demais! o/
@lemastertech
@lemastertech 2 года назад
espero que tenham gostado do vídeo
@ahmedtamer5604
@ahmedtamer5604 6 месяцев назад
But how do we not overwrite?
@lemastertech
@lemastertech 6 месяцев назад
Use A instead of W when you open the text file! That will append to the existing content rather than overwrite it!
@ahmedtamer5604
@ahmedtamer5604 6 месяцев назад
@@lemastertech thank you man
@ronnieklaase1265
@ronnieklaase1265 Год назад
How would you write your terminal output to text file?
@lemastertech
@lemastertech Год назад
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!
@mandirakaumani9036
@mandirakaumani9036 Год назад
How can we write those in each new line Like Name mark Peter 70
@lemastertech
@lemastertech Год назад
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!
@johncena-fy3mo
@johncena-fy3mo 2 года назад
Lemasters tech i need your help
@lemastertech
@lemastertech 2 года назад
Whats up?
@johncena-fy3mo
@johncena-fy3mo 2 года назад
@@lemastertech check my dm
@LimeSpeedCrystal
@LimeSpeedCrystal Год назад
And how to create a file?
@lemastertech
@lemastertech Год назад
You want to know how to create a new txt file from inside a python program??
@LimeSpeedCrystal
@LimeSpeedCrystal Год назад
@@lemastertech Yes.
@chaosforever
@chaosforever Год назад
This doesn't work dude, my python still wants to be saved as .py
@lemastertech
@lemastertech Год назад
It does work dude, what IDE do you use?
@chaosforever
@chaosforever Год назад
I use thonny@@lemastertech
@ahmedtamer5604
@ahmedtamer5604 6 месяцев назад
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', ' ')
@lemastertech
@lemastertech 6 месяцев назад
Thanks for adding this!
Далее
ТИПИЧНОЕ ПОВЕДЕНИЕ МАМЫ
00:21
Просмотров 1,4 млн
Scripting with Python - Modify a TXT file
19:00
Просмотров 51 тыс.
Python program to create and write to a Text File
4:08
5 Useful F-String Tricks In Python
10:02
Просмотров 315 тыс.