Python Crash Course: A Beginner’s Guide to Master the Basics of Python and Data Science. Learn Coding with This Machine Learning Tool. Discover the Endless Possibilities of Computers and Codes. by Wall Eric

Python Crash Course: A Beginner’s Guide to Master the Basics of Python and Data Science. Learn Coding with This Machine Learning Tool. Discover the Endless Possibilities of Computers and Codes. by Wall Eric

Author:Wall, Eric [Wall, Eric]
Language: eng
Format: epub, azw3, pdf
Published: 2020-09-27T16:00:00+00:00


Note that we had instructed the method to read only 10 bytes from the file, and that is why not all the file contents were read.

File Positions

You may use the tell() method to tell the current position in a file. This tells where the next read or write will start from the next time you attempt to do so on the file.

If you need to change this position, you can use the seek(offset[, from]) method. The argument is that offset specifies the number of bytes that should be moved. The argument from specifies the reference position from which bytes should be moved.

If the value of from is 0, then the reference position is the starting point of the file. If you set it to 1, then the current position will be used as a reference position. If it is set to 2, the file’s end will be used as a reference position.

Example

#!/usr/bin/Python3

# Openig the file

p = open("friends.txt", "r+")

str = p.read(10)

print ("The function read the string : ", str)

# Checking the current position

pos = p.tell()

print ("The current position for the file is : ", pos)

# Reposition the pointer to the beginning

pos = p.seek(0, 0)

str = p.read(10)

print ("The read String again is : ", str)

# Close the opened file

p.close()

Run the code from the directory you have stored the file names.txt in. In my case, it returns the following:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(55937)
What's Done in Darkness by Kayla Perrin(26542)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19018)
The Fifty Shades Trilogy & Grey by E L James(18988)
Shot Through the Heart by Mercy Celeste(18893)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17015)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16902)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16827)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16738)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16361)
The Subtle Art of Not Giving a F*ck by Mark Manson(14287)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14084)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13473)
Scorched Earth by Nick Kyme(12723)
Drei Generationen auf dem Jakobsweg by Stein Pia(10930)
Suna by Ziefle Pia(10856)
Scythe by Neal Shusterman(10292)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9489)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9332)
This is Going to Hurt by Adam Kay(9118)