Week 7 - How to Handle Files in Python

Liaw Bei Le · May 8, 2021

  1. open(‘filename.txt’,’r/r+/a/w/w+/x’)
  2. File attributes:
    • .readline(): Read only one line of the file
    • .readlines(): Read multiple lines of the file as a list of strings
    • .write(): Open an existing file and write to it
    • .read(): loads the content of the file into memory as a string, including formatting such as new line (\n)
    • .read(n): reading a file where n = number of characters to read
    • seek(index): go back to indicated line
  3. .close(): open file takes memory so we can use the .close() method to free resources.

Cheatsheets

Files: Guide 1 | Guide 2


What I learnt:

Create a file in VSCode:

We can create an empty file in VSCode using ‘file_name.txt’ as the format of the file name.

1. File modes for text files:

open(‘filename.txt’,’r/r+/a/w/w+/x/…’)
‘r’ - read: opens a file for reading
‘r+’ - read and write mode
‘w’ - write: overwrite the content
w+ - write: read and write mode. Overwrites existing file if file exists. If file does not exist, creates a new file for reading and writing
‘a’ - append: opens file to append to the end of the file

and there are more modes of opening files

File attributes

‘with’ and ‘as f’:
It is good practice to always use ‘with’ and ‘as f’ where possible.

a. .readline( ): Read only one line of the file

  • Reading a File and Printing its Content
  • Good Practice:
    with open('random.txt', 'r') as f:
      while True:
          line=f.readline()
          if line:
              print(line)
          else:
              break
    
  • Bad Practice,
    Don’t use this because if forget to close(), this will take up a lot of space over time.
    Just know this works too:
    text_file=open('random.txt', 'r')
    while True:
      line=text_file.readline()
      if line:
          print(line)
      else:
          break
    text_file.close()
    

b. .readlines( ): Read multiple lines of the file as a list of strings & .close( ) to close file

  • Read all lines at once.
    Good Practice:
    with open('random.txt', 'r') as fp:
      lines=fp.readlines()
      for line in lines:
          print(line)
    
  • Import file to jupyter notebook using !curl, then Read all lines.
    Good Practice:
    !curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt
    with open('poem1.txt', 'r') as f:
      poem_lines = f.readlines()
      for line in poem_lines:
          print(line)
    
  • Bad practice, avoid:
    !curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt
    poem1 = open('poem1.txt', 'r')
    poem_lines = poem1.readlines()
    for line in poem_lines:
      print(line)
    poem1.close()
    
  • Read one line at a time as a string and remove the newline character ‘\n’ for every line being read
    !curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt
    with open('poem1.txt', 'r') as f:
      poem_line = f.readline().strip('\n')
      while poem_line:
          print(poem_line.capitalize())
          poem_line = f.readline().strip('\n')  
    
  • When we use readlines to create a text file into a list of strings, it gives us unwanted newline (“\n”) characters sometimes.
    Remove newline characters from lists created using .readlines().
    • line[:-1] sets the end point at the last character of the string, the result is the ‘\n’ (newline) character is omitted
'''open file and read file as a list of strings'''
with open('poem1.txt', 'r') as f:
    poem_lines = f.readlines()
    print(poem_lines)
    '''print each list item''' 
    for line in poem_lines:
        print(line)
    '''remove the last character of each list item, which is "\n"'''
    count = 0
    for line in poem_lines:
        poem_lines[count] = line[:-1]
        count += 1
    print(poem_lines)
    '''print each list item''' 
    for line in poem_lines:
        print(line)

c. .write( ) & ‘w’

  • Write to a File
    print('Writing to File')
    list_of_countries=['Singapore', 'India', 'China', 'Japan']
    with open('country_list.txt', 'w') as f:
      for country in list_of_countries:
          f.write(country)
          f.write('\n')
    print('Done Writing to File')
    

d. .write( ) & ‘a’

  • Appending another country e.g. USA into a text file of country names
    with open('country_list.txt', 'a') as f:
      f.write('USA')
    

e. .read( )

  • Import file to jupyter notebook using !curl, then
    Read file (loads the content of the file into memory) as a string / reading a file.
    .read(n) where n is the number of characters to read
    !curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt
    with open('poem1.txt', 'r') as f:
      poem_contents = f.read()
      print(poem_contents)
      poem_contents = f.read(10)
      print(poem_contents)
    

f. .seek( ) & ‘w+’

with open('somefile.txt', 'w+') as f:
'''Note that f has now been truncated to 0 bytes, so you'll only be able to read data that you write after this point''' 
    f.write('somedata\n')
    f.seek(0)  
    '''Important: return to the top of the file before reading, otherwise you'll just read an empty string'''
    data = f.read()
  • .read( ), .readline( ) returns a string. These strings can be manipulated just like any other string.
    • Boolean tests such as:
      .upper()
      .title()
      string slices, e.g.- cities[3:9]
      etc..
    • String methods can be performed such as:
      .isdigit()
      .isalpha()
      etc…
with open('poem1.txt', 'r') as f:
    poem_part = f.read(15).upper()
    print(poem_part)

Thoughts

After learning these, I am more prepared to handle text files in Python. There were quite a number of things to know, such as the various file attributes and ways of using the different file modes. I found it good for me to have learnt the good practice vs the not so good practice, as it would help to save me some trouble in the future. I will practise more on files to get familiar with handling text files, do look out for my next post!

Twitter, Facebook