Python Programming Tutorial #13 - How to Read a Text File

Поділитися
Вставка
  • Опубліковано 27 сер 2024

КОМЕНТАРІ • 80

  • @sheyzi_
    @sheyzi_ 3 роки тому +24

    You can also do it like this
    file = open('name.txt', 'r')
    data = file.read()
    ans = data.split(
    )
    print(ans)
    It should give you the list without adding the
    in it

    • @The.Radiance
      @The.Radiance 2 роки тому

      How do you get rid of the [" "] around the text? my output is this:
      ['hello world']

    • @sheyzi_
      @sheyzi_ 2 роки тому

      @@The.Radiance Cannot really remember what I was talking about... But if it's a list you should be able to do a quick for loop and then print the string individually
      If you are sure the value will only be 1 you can just print ans[0]

  • @yusufallie3089
    @yusufallie3089 2 роки тому +6

    I honestly felt like giving up on python, you've made it so easy to understand!

  • @AliAliOxenFree
    @AliAliOxenFree Рік тому +6

    for anyone watching way in the future, windows automatically hides file extensions (such as .txt) by default. You can change this by opening any folder, clicking on the "View" tab, and checking the "File name extensions" box.

    • @notfound-vk6nc
      @notfound-vk6nc 13 днів тому +1

      Omg you saved me!! Thank u❤️
      I've been on this for two days😭✨

  • @jloo1900
    @jloo1900 2 роки тому +4

    Your are really awesome ! I had a hard time digesting these in school curriculum and you managed to enlighten me in minutes. Thank you so much.

  • @__kel
    @__kel 3 роки тому +10

    Hey all i'm using python 3.9 and this no longer works. Use the following instead.
    file = open('file.txt', 'r')
    print(file.read())
    file.close()

    • @JiggyJiggy21
      @JiggyJiggy21 2 роки тому +1

      this is so easy to understand and short at the same time! Thank you so much !

    • @quynhdiem9446
      @quynhdiem9446 2 роки тому +1

      you're the best!!! thanks

  • @user-dl4jj5sp1b
    @user-dl4jj5sp1b 5 місяців тому

    I am working on a project right now and you helped me a lot! Thank you

  • @l2dgaming490
    @l2dgaming490 4 роки тому +3

    read in 1st attempt, thank you master.

  • @BALJIT147
    @BALJIT147 2 роки тому +1

    Thanks Tim! helped me with the capture the flag game I was playing.

  • @devon6039
    @devon6039 4 роки тому +5

    You're so helpful!! Thank you!!

  • @user-ug4bg4so4r
    @user-ug4bg4so4r 6 місяців тому +1

    I keep getting an error: FileNotFoundError: [Errno 2] No such file or directory: 'file.txt' but I checked the files place and the name and it should be al fine. are there other reasons why this doesn't work?

  • @HeroesOfAesthetics
    @HeroesOfAesthetics 3 роки тому

    great video Tim!! Just what I needed!

  • @Creativesnooker
    @Creativesnooker 2 роки тому

    Found your channel yesterday and subscribed... just starting my python journey so I am a bit late to the party lol , been at it a few weeks now and find your style of teaching is helping things stick so thank you. Subbed :)

    • @aphelelembukwana9146
      @aphelelembukwana9146 7 місяців тому +1

      you were earlier than the next person which is me .....hope you still going and for sure you good at it now

  • @basicallybrand
    @basicallybrand 3 роки тому +4

    I don't get this

  • @petrosstyle2981
    @petrosstyle2981 3 роки тому +3

    "r" argument is not necessary in the open() function because r is the default option.

  • @jacksonwing5789
    @jacksonwing5789 Рік тому

    Only needed the first 1;23 seconds of the video but thank ya very much mister

  • @Jeff--vq4zb
    @Jeff--vq4zb Рік тому

    I just LOL'd when Tim wrote "easy" at the end but didn't hit enter to leave the EOF (end of file) at the bottom, and a newline character after "easy". As a teenager I developed a habit of always having a blank line at the bottom, and this video is why. Tim's tutorial is good though, he demonstrates how to handle any scenario. Next lesson: why to never use whitespace characters on your files ;-)

  • @khomikoow5994
    @khomikoow5994 2 роки тому

    Recommend using the "with" key to open and write.

  • @jonnymue6869
    @jonnymue6869 4 роки тому

    well thx man for the great introduction into learning how to code.

  • @ruarkpotgieter7891
    @ruarkpotgieter7891 5 років тому +17

    now this is epic!!

  • @user-rq9kd7xu5x
    @user-rq9kd7xu5x 3 роки тому +2

    Thank you 🤩🙏🏼

  • @TheDickw
    @TheDickw 4 роки тому +1

    Great Video.
    Thank You.

  • @hamoodhabibi7026
    @hamoodhabibi7026 5 років тому +6

    my internet !!!! it keeps buffering T^T..... i have to watch in 360p

  • @tomasmartini105
    @tomasmartini105 4 роки тому +2

    Perfect for beginners like me! Ty!

    • @isaacirwin8628
      @isaacirwin8628 4 роки тому +2

      I suggest you use
      file .close()
      on line 3 so you avoid over loading your ram.

  • @DunjaLife
    @DunjaLife 4 роки тому +2

    after I put -1 first time it doesn't work anymore :( (3.8 version)

  • @mrcoldshower465
    @mrcoldshower465 2 роки тому +1

    This is outdated, I recommend watching the 'Python Tutorial: How to Read and Write Text Files' tutorial, from Essential Engineering. His code works fine

  • @abdullahmamun1777
    @abdullahmamun1777 2 роки тому +1

    Can we create 5 folders {hello,tim, python,learning,easy} reading from text file??

  • @EricRobb
    @EricRobb 3 роки тому

    Great Video!

  • @dwaynethedistinguiseddinos1968
    @dwaynethedistinguiseddinos1968 2 роки тому

    Do you have a video to show how to path join with files (txt in my case)? If so could you please show me/anyone else that needs this video? Thank you for this video as well.

  • @hackercop
    @hackercop 3 роки тому

    When using linux python always places my text file in /home/myUsername directory instead of next to the .py file, why is this?

  • @Sciencedoneright
    @Sciencedoneright 3 роки тому

    1:02 It's not random
    It's the description of your series

  • @nashfpv2986
    @nashfpv2986 3 роки тому

    Can you use .splitlines() to remove the
    ?

  • @damonchase1922
    @damonchase1922 3 роки тому

    line is just your word choice for item correct?

  • @haris7521
    @haris7521 2 роки тому

    Just awesome

  • @emanuelfolorunsho9881
    @emanuelfolorunsho9881 10 місяців тому

    I'm putting together a quiz for an assignment, and it requires the use of a text file.

  • @kyleyoung2768
    @kyleyoung2768 2 роки тому

    can someone give me an example of what this could be used for when working on a project?

  • @vorador4365
    @vorador4365 5 років тому +3

    So
    = 1 character? Did not expect that hehe

  • @eswarspython2743
    @eswarspython2743 4 роки тому

    Nice video
    Watched at 2x
    Meet you in next one

  • @smokedd9052
    @smokedd9052 2 роки тому

    it's always saying No such file when I made the file already and I made it in Quick access

  • @Mythologos
    @Mythologos 2 роки тому

    "name read is not defined".

  • @nauji
    @nauji 4 роки тому

    Awesome !

  • @shitoshitt
    @shitoshitt 3 роки тому +1

    which mode can i use to read as well as write in a text file. this to save and access data of a game

  • @mskyba
    @mskyba 4 роки тому +2

    file.read() is a lot easier

    • @isaiah7310
      @isaiah7310 4 роки тому +1

      I just tested that out a few different ways. It looks like file.read() returns one large string while the file.readlines() returns each line as a separate string, which makes it easier to manipulate into a list.

  • @isaacirwin8628
    @isaacirwin8628 4 роки тому +1

    He didn't "file.close()" which means his ram will fill up really quickly.

    • @isaiah7310
      @isaiah7310 4 роки тому

      Looks like it auto-closes for me. Each time I used the file.readlines() I had to reopen the file if I wanted to call it in another instance.

    • @tinpot1978
      @tinpot1978 3 роки тому +1

      @@isaiah7310 It doesn't auto-close using the method he used to open. You should use "with open("file.txt", "r").

    • @isaiah7310
      @isaiah7310 3 роки тому

      @@tinpot1978 Thanks

  • @MrRahul15937
    @MrRahul15937 2 роки тому

    I used rstrip('
    ')

  • @armadeuzmaze3675
    @armadeuzmaze3675 3 роки тому

    how to do math with files?

  • @yaboyisbadatfortnut3158
    @yaboyisbadatfortnut3158 4 роки тому

    not working on thonny. someone help please

  • @basicallybrand
    @basicallybrand 3 роки тому

    I thought append meant add

  • @anilterapalli3894
    @anilterapalli3894 2 роки тому

    I saved both textfile & idle file in same folder,
    even after that it shows me file not found😐

    • @Polshani_keshf_et
      @Polshani_keshf_et 2 роки тому

      having the same issue. Could you solve it?

    • @tomoe6227
      @tomoe6227 2 роки тому

      @@Polshani_keshf_et why not post the code here.... i can help

  • @procerusgigas
    @procerusgigas 5 років тому +1

    So why the hell do you start a tutorial in python 3.6 and than in the middle you switch to 3.5 WITH NO WARNING??? Just lost HOURS because I thought I went crazy, had the same code but it wasn't working....

    • @TechWithTim
      @TechWithTim  5 років тому +7

      What code did I use that works in 3.6 that doesn’t work In 3.5 or other way around

  • @justifyl
    @justifyl 9 днів тому

    this was an terrible jump from vid 12 to 13 , feels like a new world..

  • @sri4sg3
    @sri4sg3 2 роки тому

    gold

  • @mukeshkanwarmukeshkanwar2749

    ऐप का क्या नाम है

  • @leobozkir5425
    @leobozkir5425 3 роки тому

    "random words"

  • @shantanupatil7323
    @shantanupatil7323 2 роки тому

    iluvu

  • @abhijatpandey1490
    @abhijatpandey1490 4 роки тому +1

    huh...

  • @MinerMends
    @MinerMends 4 роки тому

    your code is wrong lol and outdated

    • @garrybeck216
      @garrybeck216 4 роки тому +4

      Instead of making unhelpful comments, how about telling us why it's wrong and outdated?

    • @templetherapy
      @templetherapy 4 роки тому +2

      can you tell me please becuase mine is not working

    • @prettylou2010
      @prettylou2010 4 роки тому +2

      the code does work guys.. are you putting [ ] when you start the newList or you putting () ?
      you have to put [ ] not ()

    • @WildclawsDK
      @WildclawsDK 4 роки тому

      @@prettylou2010 Works for me, P3.8

    • @Sciencedoneright
      @Sciencedoneright 3 роки тому

      I'm P 3.9 and it's working fine