Python read file

Python read file

Python read file allows us to read contents of file in very easy way. If we want to read some contents to file, first of all we need to open a file and create object of that file. With that object we can read() a file.

In our previous article have discussed about file handling  and its various methods . If you want to learn it then you can visit link below:

Python file handling

Now we will discuss about various other operations on  files like reading , renaming a file etc. Let’s talk about reading a file.

Python  read file ( read() )

Python allows us to read contents of file in very easy way. If we want to read some contents to file, first of all we need to open a file and create object of that file. With that object we can read() a file.  We can read:

  • According to size
  • Whole file.

Python read file : According to size (particular contents)

We can read particular content of file by passing size as its argument. The size is in number of bytes we want to read from a file. In this, pointer’s first location is on the beginning  of file and last location points to the size given (bytes).

Syntax

objname.read([size in bytes ])

Python read file : Entire file

If we does not specify size to read() then it will read all the contents of the file till end. In this, pointer’s first location is on the beginning  of file and last location points the last element of file.

Syntax

objname.read()

Example

python read file
python read file

As you can see in the example above, In line 2-5, i have read only 10 bytes (characters including spaces) , so it prints  “I can delete” as output . And in line 8-12 , i have read entire file without specifying size (bytes) to read.

Python read file : Read line ( readline() )

You can read single line of the file by calling readline() method. If you want to read more than one line, you can use readline() method number of times , you want to read lines.

For example , if you want to read first two lines from a file, then you can do so by calling readline() function two times.

Example

python read file
python read file

Output

output: python read file
output: python read file

 

Python read file :Looping through lines

To read a file, we can also put loop through all the elements of file and print on console. Here is the code for looping through elements of file .

Example

python read file
python read file

Output

output: read file
output: read file

Python close() method

You may see in our previous article, i used close() method there. So what close() is?

We use close() method to close particular file object, or opened file. Once close() method is executed, we can’t further write or read particular file object.  Python automatically closes a file, once we create the other object (instance) of same file. After closing file object, if we try to read/write that object, it returns error.

Example

python close file
python close file

Output

output: python close() file
output: python close() file

As you can see, in example above, i am trying to write contents on file , after closing it. So it cause to return an error i.e.

ValueError: I/O operation on closed file.

If you want to learn more about file handling , deleting , renaming files than you can visit the link below:

Python OS Module

So it is all about Python read file. We will discuss some more methods of Python file handling . i hope you guys enoyed it, thanks!!!

148 thoughts on “Python read file

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Apple now has Rhapsody as an app, which is a great start, but it is currently hampered by the inability to store locally on your iPod, and has a dismal 64kbps bit rate. If this changes, then it will somewhat negate this advantage for the Zune, but the 10 songs per month will still be a big plus in Zune Pass’ favor.

  2. The Zune concentrates on being a Portable Media Player. Not a web browser. Not a game machine. Maybe in the future it’ll do even better in those areas, but for now it’s a fantastic way to organize and listen to your music and videos, and is without peer in that regard. The iPod’s strengths are its web browsing and apps. If those sound more compelling, perhaps it is your best choice.

  3. Zune and iPod: Most people compare the Zune to the Touch, but after seeing how slim and surprisingly small and light it is, I consider it to be a rather unique hybrid that combines qualities of both the Touch and the Nano. It’s very colorful and lovely OLED screen is slightly smaller than the touch screen, but the player itself feels quite a bit smaller and lighter. It weighs about 2/3 as much, and is noticeably smaller in width and height, while being just a hair thicker.

  4. If you’re still on the fence: grab your favorite earphones, head down to a Best Buy and ask to plug them into a Zune then an iPod and see which one sounds better to you, and which interface makes you smile more. Then you’ll know which is right for you.

  5. If you’re still on the fence: grab your favorite earphones, head down to a Best Buy and ask to plug them into a Zune then an iPod and see which one sounds better to you, and which interface makes you smile more. Then you’ll know which is right for you.

  6. Apple now has Rhapsody as an app, which is a great start, but it is currently hampered by the inability to store locally on your iPod, and has a dismal 64kbps bit rate. If this changes, then it will somewhat negate this advantage for the Zune, but the 10 songs per month will still be a big plus in Zune Pass’ favor.

  7. Zune and iPod: Most people compare the Zune to the Touch, but after seeing how slim and surprisingly small and light it is, I consider it to be a rather unique hybrid that combines qualities of both the Touch and the Nano. It’s very colorful and lovely OLED screen is slightly smaller than the touch screen, but the player itself feels quite a bit smaller and lighter. It weighs about 2/3 as much, and is noticeably smaller in width and height, while being just a hair thicker.

  8. Hands down, Apple’s app store wins by a mile. It’s a huge selection of all sorts of apps vs a rather sad selection of a handful for Zune. Microsoft has plans, especially in the realm of games, but I’m not sure I’d want to bet on the future if this aspect is important to you. The iPod is a much better choice in that case.

  9. Sorry for the huge review, but I’m really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it’s the right choice for you.

  10. Between me and my husband we’ve owned more MP3 players over the years than I can count, including Sansas, iRivers, iPods (classic & touch), the Ibiza Rhapsody, etc. But, the last few years I’ve settled down to one line of players. Why? Because I was happy to discover how well-designed and fun to use the underappreciated (and widely mocked) Zunes are.

  11. I’ll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X, but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)

  12. Zune and iPod: Most people compare the Zune to the Touch, but after seeing how slim and surprisingly small and light it is, I consider it to be a rather unique hybrid that combines qualities of both the Touch and the Nano. It’s very colorful and lovely OLED screen is slightly smaller than the touch screen, but the player itself feels quite a bit smaller and lighter. It weighs about 2/3 as much, and is noticeably smaller in width and height, while being just a hair thicker.

  13. What’s Taking place i’m new to this, I stumbled upon this I’ve found It absolutely useful and it has helped me out loads. I am hoping to give a contribution & assist different users like its aided me. Good job.

  14. Thank you for sharing excellent informations. Your web-site is very cool. I’m impressed by the details that you have on this web site. It reveals how nicely you understand this subject. Bookmarked this website page, will come back for more articles. You, my pal, ROCK! I found just the info I already searched all over the place and simply couldn’t come across. What a great website.

  15. If you’re still on the fence: grab your favorite earphones, head down to a Best Buy and ask to plug them into a Zune then an iPod and see which one sounds better to you, and which interface makes you smile more. Then you’ll know which is right for you.

  16. I loved as much as you will receive carried out right here. The sketch is tasteful, your authored subject matter stylish. nonetheless, you command get got an edginess over that you wish be delivering the following. unwell unquestionably come more formerly again as exactly the same nearly very often inside case you shield this increase.

  17. Hello There. I found your blog using msn. This is an extremely well written article. I will make sure to bookmark it and come back to read more of your useful info. Thanks for the post. I will certainly return.

  18. I simply want to tell you that I’m beginner to blogs and actually liked your blog. Very likely I’m going to bookmark your blog . You actually come with awesome posts. Thanks a bunch for sharing with us your blog.

  19. We still cannot quite think that Possible come to be those types of checking important points positioned on your webblog. Our grandkids and i also are sincerely thankful for your special generosity as well as for giving me possibility pursue our chosen profession path. Basically right information I managed to get from your own web-site.

  20. Youre so cool! I dont suppose Ive learn anything like this before. So nice to search out somebody with some unique ideas on this subject. realy thank you for beginning this up. this website is one thing that is wanted on the web, somebody with a little bit originality. useful job for bringing something new to the internet!

  21. The new Zune browser is surprisingly good, but not as good as the iPod’s. It works well, but isn’t as fast as Safari, and has a clunkier interface. If you occasionally plan on using the web browser that’s not an issue, but if you’re planning to browse the web alot from your PMP then the iPod’s larger screen and better browser may be important.

  22. Hands down, Apple’s app store wins by a mile. It’s a huge selection of all sorts of apps vs a rather sad selection of a handful for Zune. Microsoft has plans, especially in the realm of games, but I’m not sure I’d want to bet on the future if this aspect is important to you. The iPod is a much better choice in that case.

  23. Zune and iPod: Most people compare the Zune to the Touch, but after seeing how slim and surprisingly small and light it is, I consider it to be a rather unique hybrid that combines qualities of both the Touch and the Nano. It’s very colorful and lovely OLED screen is slightly smaller than the touch screen, but the player itself feels quite a bit smaller and lighter. It weighs about 2/3 as much, and is noticeably smaller in width and height, while being just a hair thicker.

  24. Sorry for the huge review, but I’m really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it’s the right choice for you.

  25. Hands down, Apple’s app store wins by a mile. It’s a huge selection of all sorts of apps vs a rather sad selection of a handful for Zune. Microsoft has plans, especially in the realm of games, but I’m not sure I’d want to bet on the future if this aspect is important to you. The iPod is a much better choice in that case.

  26. Sorry for the huge review, but I’m really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it’s the right choice for you.

  27. The Zune concentrates on being a Portable Media Player. Not a web browser. Not a game machine. Maybe in the future it’ll do even better in those areas, but for now it’s a fantastic way to organize and listen to your music and videos, and is without peer in that regard. The iPod’s strengths are its web browsing and apps. If those sound more compelling, perhaps it is your best choice.

  28. The Zune concentrates on being a Portable Media Player. Not a web browser. Not a game machine. Maybe in the future it’ll do even better in those areas, but for now it’s a fantastic way to organize and listen to your music and videos, and is without peer in that regard. The iPod’s strengths are its web browsing and apps. If those sound more compelling, perhaps it is your best choice.

  29. I do not even know how I ended up here, but I thought this post was great. I do not know who you are but certainly you’re going to a famous blogger if you are not already 😉 Cheers!

  30. Hey would you mind letting me know which webhost you’re using? I’ve loaded your blog in 3 different internet browsers and I must say this blog loads a lot quicker then most. Can you suggest a good hosting provider at a fair price? Thank you, I appreciate it!

  31. Between me and my husband we’ve owned more MP3 players over the years than I can count, including Sansas, iRivers, iPods (classic & touch), the Ibiza Rhapsody, etc. But, the last few years I’ve settled down to one line of players. Why? Because I was happy to discover how well-designed and fun to use the underappreciated (and widely mocked) Zunes are.

  32. If you’re still on the fence: grab your favorite earphones, head down to a Best Buy and ask to plug them into a Zune then an iPod and see which one sounds better to you, and which interface makes you smile more. Then you’ll know which is right for you.

  33. Apple now has Rhapsody as an app, which is a great start, but it is currently hampered by the inability to store locally on your iPod, and has a dismal 64kbps bit rate. If this changes, then it will somewhat negate this advantage for the Zune, but the 10 songs per month will still be a big plus in Zune Pass’ favor.

  34. Hi! I know this is kinda off topic however , I’d figured I’d ask. Would you be interested in trading links or maybe guest authoring a blog article or vice-versa? My blog discusses a lot of the same subjects as yours and I believe we could greatly benefit from each other. If you are interested feel free to send me an email. I look forward to hearing from you! Excellent blog by the way!

  35. Sorry for the huge review, but I’m really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it’s the right choice for you.

  36. Great post. I used to be checking continuously this blog and I am inspired! Extremely useful info particularly the ultimate part 🙂 I take care of such information much. I used to be looking for this particular information for a long time. Thank you and good luck.

  37. I don’t even understand how I stopped up right here, however I believed this publish
    was once great. I do not know who you’re but certainly you’re going
    to a well-known blogger for those who aren’t already.
    Cheers!

  38. My coder is trying to convince me to move to .net from PHP.
    I have always disliked the idea because of the costs.
    But he’s tryiong none the less. I’ve been using Movable-type on various websites for about a year and am worried about switching to another platform.
    I have heard great things about blogengine.net.
    Is there a way I can import all my wordpress content into it?
    Any help would be greatly appreciated!

  39. Thanks for every other excellent article. The place else could anybody get that kind of info in such a perfect manner of writing?
    I’ve a presentation next week, and I am on the search for
    such info.

  40. You’re so awesome! I don’t think I have read through anything like this before.
    So wonderful to find someone with a few genuine thoughts on this issue.
    Really.. thanks for starting this up. This site is something
    that is needed on the internet, someone with a little originality!

  41. Hey there! This is kind of off topic but I need some advice from an established blog.

    Is it very difficult to set up your own blog?
    I’m not very techincal but I can figure things out pretty quick.
    I’m thinking about creating my own but I’m not sure where to begin. Do you have any points or
    suggestions? Thanks

  42. My brother recommended I might like this website. He was totally right.
    This post actually made my day. You cann’t
    imagine simply how much time I had spent for this info!
    Thanks!

  43. At this time it appears like Drupal is the best blogging platform available right
    now. (from what I’ve read) Is that what you’re using on your blog?

  44. Hello there! This blog post couldn’t be written much better!
    Looking through this article reminds me of my previous roommate!

    He constantly kept talking about this. I am going to forward this post to him.
    Fairly certain he will have a great read. Thank you for sharing!

  45. “Hi! I’ve been reading your weblog for a long time now and finally got the courage to go ahead and give you a shout out from Houston Tx! Just wanted to mention keep up the excellent work!”

  46. Hello very cool blog!! Man .. Beautiful .. Wonderful .. I’ll bookmark your web site and take the feeds also?KI am satisfied to search out so many helpful information here in the publish, we want develop more strategies on this regard, thanks for sharing. . . . . .

  47. “That is really attention-grabbing, You are an excessively professional blogger. I have joined your feed and look ahead to seeking more of your great post. Additionally, I’ve shared your web site in my social networks!”

  48. “Nice post. I study something tougher on different blogs everyday. It should always be stimulating to learn content material from different writers and follow somewhat something from their store. I’d prefer to make use of some with the content material on my blog whether you don’t mind. Natually I’ll give you a hyperlink on your web blog. Thanks for sharing.”

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.