ไม่สามารถเล่นวิดีโอนี้
ขออภัยในความไม่สะดวก

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

ความคิดเห็น • 87

  • @kanicabansal937
    @kanicabansal937 ปีที่แล้ว +3

    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  ปีที่แล้ว +1

      You are so welcome I’m glad it was so useful for you!! Thanks for watching :)

  • @Dj-ij6nf
    @Dj-ij6nf ปีที่แล้ว +2

    Thanks Boss, u just saved my life 😺

    • @lemastertech
      @lemastertech  ปีที่แล้ว

      Haha you're very welcome my friend! Thanks for watching!

  • @mythorium8143
    @mythorium8143 ปีที่แล้ว +3

    Thanks so much, you helped me complete my computing assignment :)

    • @lemastertech
      @lemastertech  ปีที่แล้ว

      I’m so glad it helped you, thanks for watching!!

  • @ahmedtamer5604
    @ahmedtamer5604 4 หลายเดือนก่อน +1

    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  4 หลายเดือนก่อน

      Thanks for the useful comment! Hopefully others find this too if they have the same question :)

  • @senile8668
    @senile8668 4 หลายเดือนก่อน +1

    Thank you so much for the help. I needed this info for my uni assignment
    You’re the best!

    • @lemastertech
      @lemastertech  4 หลายเดือนก่อน

      Hey you are very welcome! Thanks for watching!!

  • @yasr5931
    @yasr5931 ปีที่แล้ว +5

    This was super easy to understand and really helpful! Thanks loads :)

    • @lemastertech
      @lemastertech  ปีที่แล้ว +1

      You are super welcome!! Glad it was useful for you my friend :)

  • @gnostie
    @gnostie ปีที่แล้ว +5

    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  ปีที่แล้ว +1

      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!

  • @goldenfox334
    @goldenfox334 2 ปีที่แล้ว +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!

  • @nathanblackburn5055
    @nathanblackburn5055 ปีที่แล้ว +2

    Great video. Explained very well

    • @lemastertech
      @lemastertech  ปีที่แล้ว

      Glad you liked it, thanks for watching!!

  • @NSKP_
    @NSKP_ 2 ปีที่แล้ว +2

    Thank you!! it helped much🙂

    • @lemastertech
      @lemastertech  2 ปีที่แล้ว +1

      You are welcome!! So glad you found it useful!!

  • @Spugler2
    @Spugler2 8 หลายเดือนก่อน +2

    I copied everything you did exactly and nothing happens. No errors, the file doesn't open, and nothing gets written.

    • @lemastertech
      @lemastertech  8 หลายเดือนก่อน

      Interesting - it might be getting written/saved somewhere else on your laptop. What IDE are you using??

    • @Spugler2
      @Spugler2 8 หลายเดือนก่อน

      @@lemastertech visual studio code

  • @tarikselim499
    @tarikselim499 ปีที่แล้ว +3

    Thanks a lot :) Can u make more python videos pls 🤗 it was really helpfull and easy to understand

    • @lemastertech
      @lemastertech  ปีที่แล้ว +2

      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! :)

  • @kirillzakharov7336
    @kirillzakharov7336 2 หลายเดือนก่อน +1

    How would you alter that final code so that the file would be read from the middle to the end?

    • @lemastertech
      @lemastertech  2 หลายเดือนก่อน

      You can just read in the text file, check total length or total number of lines and then only work with the second half?

  • @fazdefaz
    @fazdefaz ปีที่แล้ว +1

    you explained things better than my lecturer. What about writing to a txt file with password attempts and time and date?

    • @lemastertech
      @lemastertech  ปีที่แล้ว +1

      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!

  • @xX_NameHere_Xx
    @xX_NameHere_Xx ปีที่แล้ว +1

    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!

  • @NoongarCulture
    @NoongarCulture ปีที่แล้ว +1

    awesome, thanks!

    • @lemastertech
      @lemastertech  ปีที่แล้ว

      You’re welcome!! Glad it worked for you!

  • @FPSManInTheWall
    @FPSManInTheWall 11 หลายเดือนก่อน +1

    Many thanks

    • @lemastertech
      @lemastertech  11 หลายเดือนก่อน

      You’re welcome thanks for watching!!

  • @ufuomaomo-enwefah8129
    @ufuomaomo-enwefah8129 9 หลายเดือนก่อน +1

    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  9 หลายเดือนก่อน +1

      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!

  • @3pm-on-yt
    @3pm-on-yt ปีที่แล้ว +1

    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 th-cam.com/video/_N8_BhcGHxU/w-d-xo.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

  • @haziqkepli1608
    @haziqkepli1608 10 หลายเดือนก่อน +1

    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  10 หลายเดือนก่อน +1

      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 10 หลายเดือนก่อน

      thank you that helped so much!! :D@@lemastertech

  • @audio_masterenghinsongs4898
    @audio_masterenghinsongs4898 4 หลายเดือนก่อน +1

    How to take input and then write the input data in that file?

    • @lemastertech
      @lemastertech  4 หลายเดือนก่อน +1

      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 4 หลายเดือนก่อน

      @@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.

  • @rowcezarbrazil
    @rowcezarbrazil ปีที่แล้ว +1

    What about create a new file instead of edit an existing one? Is it possible?

    • @lemastertech
      @lemastertech  ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      @@lemastertech Thanks very much!!

    • @lemastertech
      @lemastertech  ปีที่แล้ว

      @@rowcezarbrazil you got it!

  • @gautammazumdar5643
    @gautammazumdar5643 2 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      @@lemastertech Thanks a lot sir. If I face doubt again i will again ask you sir

    • @lemastertech
      @lemastertech  2 ปีที่แล้ว

      @@gautammazumdar5643 good luck!

  • @jrnastyy
    @jrnastyy 2 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      @@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 ปีที่แล้ว +1

      @@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 ปีที่แล้ว +1

      @@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!!!

  • @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  ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      @@lemastertech Thank you!

    • @lemastertech
      @lemastertech  ปีที่แล้ว

      @@adithyamadhu2309 You're welcome, thank you for watching!

  • @ronnieklaase1265
    @ronnieklaase1265 11 หลายเดือนก่อน

    How would you write your terminal output to text file?

    • @lemastertech
      @lemastertech  11 หลายเดือนก่อน

      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!

  • @ahmedtamer5604
    @ahmedtamer5604 4 หลายเดือนก่อน +1

    But how do we not overwrite?

    • @lemastertech
      @lemastertech  4 หลายเดือนก่อน

      Use A instead of W when you open the text file! That will append to the existing content rather than overwrite it!

    • @ahmedtamer5604
      @ahmedtamer5604 4 หลายเดือนก่อน +1

      @@lemastertech thank you man

  • @robertcristian274
    @robertcristian274 ปีที่แล้ว +1

    goood !

    • @lemastertech
      @lemastertech  ปีที่แล้ว

      Thank you! Thanks for watching!

  • @victorcandiani
    @victorcandiani 2 ปีที่แล้ว +1

    Salvou demais! o/

    • @lemastertech
      @lemastertech  2 ปีที่แล้ว

      espero que tenham gostado do vídeo

  • @mandirakaumani9036
    @mandirakaumani9036 ปีที่แล้ว +2

    How can we write those in each new line
    Like
    Name mark
    Peter 70

    • @lemastertech
      @lemastertech  11 หลายเดือนก่อน

      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!

  • @chaosforever
    @chaosforever 10 หลายเดือนก่อน +1

    This doesn't work dude, my python still wants to be saved as .py

    • @lemastertech
      @lemastertech  10 หลายเดือนก่อน

      It does work dude, what IDE do you use?

    • @chaosforever
      @chaosforever 10 หลายเดือนก่อน

      I use thonny@@lemastertech

  • @LimeSpeedCrystal
    @LimeSpeedCrystal ปีที่แล้ว +1

    And how to create a file?

    • @lemastertech
      @lemastertech  ปีที่แล้ว +1

      You want to know how to create a new txt file from inside a python program??

    • @LimeSpeedCrystal
      @LimeSpeedCrystal ปีที่แล้ว

      @@lemastertech Yes.

  • @johncena-fy3mo
    @johncena-fy3mo 2 ปีที่แล้ว

    Lemasters tech i need your help

  • @ahmedtamer5604
    @ahmedtamer5604 4 หลายเดือนก่อน +1

    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  4 หลายเดือนก่อน

      Thanks for adding this!