Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Python Programming Essentials - M22 - File Operations

753 views

Published on

Slides from the training on the topic "Python Programming Essentials"

  • Be the first to comment

Python Programming Essentials - M22 - File Operations

  1. 1. http://www.skillbrew.com /SkillbrewTalent brewed by the industry itself File operations in python Pavan Verma @YinYangPavan 1 Founder, P3 InfoTech Solutions Pvt. Ltd. Python Programming Essentials
  2. 2. © SkillBrew http://skillbrew.com Note  Please download file alice.txt from https://dl.dropboxusercontent.com/u/40996392/Hap piestMinds/alice.txt for examples in this module 2
  3. 3. © SkillBrew http://skillbrew.com Open a file 3 f = open('alice.txt', 'r') print f.read() f.close()
  4. 4. © SkillBrew http://skillbrew.com Open a File (2) Output: Well!' thought Alice to herself, 'after such a fall as this, I shall think nothing of tumbling down stairs! How brave they'll all think me at home! Why, I wouldn't say anything about it, even if I fell off the top of the house!' (Which was very likely true.) 4
  5. 5. © SkillBrew http://skillbrew.com Open a file (3) 5 • open function call takes in two parameters filename, mode • filename can be a absolute path or a relative path • On linux: open('/home/xyz/workspace/alice.txt', 'r') • On windows: open('C:Usersxyzworkspacealice.txt', 'r') • If just filename is added python will look for file in current directory where the program is being executed
  6. 6. © SkillBrew http://skillbrew.com Open a file (4) 6 f = open('alice.txt', 'r') • mode is the mode in which you want to open the file There are mainly three modes 1. Read 2. Write 3. Append • By default the mode is Read
  7. 7. © SkillBrew http://skillbrew.com Mode Details r File will only be read. The file pointer is placed at the beginning of the file. This is the default mode. w Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.. a Opens a file for appending. The file pointer is at the end of the file if the file exists. If the file does not exist, it creates a new file for writing. File modes 7
  8. 8. © SkillBrew http://skillbrew.com Mode Details r+ Opens a file for both reading and writing. The file pointer will be at the beginning of the file w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. File modes (2) 8
  9. 9. © SkillBrew http://skillbrew.com Methods of File Objects  read()  readline()  readlines()  write()  seek()  tell() 9
  10. 10. © SkillBrew http://skillbrew.com read() 10 f = open('alice.txt', 'r') print f.read() f.close() read(size) • size is an optional argument, If size is omitted or negative entire file contents are read at once • If end of file has been reached f.read()returns empty string
  11. 11. © SkillBrew http://skillbrew.com read() (2) 11 f = open('alice.txt', 'r') print f.read(100) f.close() Output: Well!' thought Alice to herself, 'after such a fall as this, I shall think nothing of tumbling down
  12. 12. © SkillBrew http://skillbrew.com readline() 12 f = open('alice.txt', 'r') print f.readline() f.close() Output: Well!' thought Alice to herself, 'after such a fall as this,
  13. 13. © SkillBrew http://skillbrew.com readline() (2) 13 f = open('alice.txt', 'r') print f.readline() f.close() • Reads a single line from the file • If f.readline() returns an empty string, the end of the file has been reached
  14. 14. © SkillBrew http://skillbrew.com readlines() 14 f = open('alice.txt', 'r') lines = f.readlines() print lines f.close() readlines() reads the entire file and returns a list of lines
  15. 15. © SkillBrew http://skillbrew.com readlines() (2) 15 Output: ["Well!' thought Alice to herself, 'after such a fall as this,n", 'I shall think nothing of tumbling down stairs!n', "How brave they'll all think me at home!n", "Why, I wouldn't say anything about it,n", "even if I fell off the top of the house!'n", '(Which was very likely true.)']
  16. 16. © SkillBrew http://skillbrew.com readlines() (2) 16 f = open('alice.txt', 'r') lines = f.readlines() print lines for line in lines: print line f.close()
  17. 17. © SkillBrew http://skillbrew.com write() 17 text = "The White Rabbit appears again in search of the Duchess's gloves and fan" f = open('wonderland.txt', 'w') f.write(text) f.close() write(str) writes a string of characters to a file • wonderland.txt is created if it did not exist • If wonderland.txt existed then it would be overwritten by this text
  18. 18. © SkillBrew http://skillbrew.com write() (2) 18 Output: wonderland.txt The White Rabbit appears again in search of the Duchess's gloves and fan
  19. 19. © SkillBrew http://skillbrew.com write() (3) 19 f1 = open('alice.txt', 'r') text = f1.read() f2 = open('wonderland.txt', 'w') f2.write(text) f2.close() f1.close() Reads text from alice.txt and writes to wonderland.txt
  20. 20. © SkillBrew http://skillbrew.com write() 20 Ouput: wonderland.txt Well!' thought Alice to herself, 'after such a fall as this, I shall think nothing of tumbling down stairs! How brave they'll all think me at home! Why, I wouldn't say anything about it, even if I fell off the top of the house!' (Which was very likely true.)
  21. 21. © SkillBrew http://skillbrew.com append 21 text = "The White Rabbit appears again in search of the Duchess's gloves and fan" f = open('wonderland.txt', 'a') f.write(text) f.close() Opening the file in append mode will move the file pointer to end of the file
  22. 22. © SkillBrew http://skillbrew.com append (2) 22 Ouput: wonderland.txt Well!' thought Alice to herself, 'after such a fall as this, I shall think nothing of tumbling down stairs! How brave they'll all think me at home! Why, I wouldn't say anything about it, even if I fell off the top of the house!' (Which was very likely true.)The White Rabbit appears again in search of the Duchess's gloves and fan
  23. 23. © SkillBrew http://skillbrew.com seek 23 f = open('wonderland.txt') f.seek(7) print f.readline() f.close() Output: thought Alice to herself, 'after such a fall as this, seek(offset) changes the file object position to offset The offset by default is measured form beginning of file
  24. 24. © SkillBrew http://skillbrew.com tell 24 f = open('alice.txt', 'r') f.seek(7) # changes file pos to offset 7 print f.readline() print f.tell() # tells the current pos Output: thought Alice to herself, 'after such a fall as this, I shall 62 tell() returns an integer giving the file object’s current position
  25. 25. © SkillBrew http://skillbrew.com Summary  Open a file  Methods of file objects (read, readline, readlines, write, seek, tell) 25
  26. 26. © SkillBrew http://skillbrew.com Resources  reading and writing files http://docs.python.org/2/tutorial/inputoutput.html#reading- and-writing-files  Methods on file objects http://docs.python.org/2/tutorial/inputoutput.html#methods- of-file-objects 26

×