My QA Projects

QA Projects I was involded.

View on GitHub

Reading Text Files

Syntax

fileopbj = open(filename, mode)
mode operation
‘r’ read
‘w’ write
‘a’ append
‘x’ write but only if it doesn exists
‘t’ text
‘b’ binary

File Objects

open() function

You need to cal the open() before you:

Syntax

open(filename, mode='r')
operation mode
‘r’ read
‘w’ write
‘a’ append

‘a’ “a” - Append - will append to the end of the file “w” - Write - will overwrite any existing content

Write a Textfile with print()

fout = open(oops.txt, 'wt')
print('Oops, I created a file.', file=fout)
fout.close()

Using file argument to print

without the file argument to print, print just writes to standard output > terminal. [unless you have told the shell program to redirect output to a file with > or piped it to another program with | ]

Write a Textfile with write()

poem = '''There was a yong lady named Bright, 
Whose pseed was faster then light;'''

fout = open('speed', 'wt')
fout.write(poem)
# Output
fout.close()

Write() returns the number od bytes written. It does not add any spaces or newlines, as print does. To make print() work like write(), you pass the arguments sep - called seperator (‘ ‘) and end called end string (\n)

poem = '''There was a yong lady named Bright, 
Whose pseed was faster then light;'''

fout = open('speed', 'wt')
print(poem, file=fout)
# print(poem, file=fout sep'', end='')
fout.close()
try:
  fout = open('relativity', 'xt')
  fout.write('stomp, stromp, stomp')
excpt FileExistsError:
  print("relativity already exists. overriding prevented")

Getting a File Object (read mode)

f = open(filename, mode='r')
file_access_codeblock
f.close()

closing the file object releases resources

Context manager

Syntax

with open(filename, 'r') as f:
    file_access_codeblock

Read method

Syntax

f.read()

Example1

f = open("demofile.txt", "r")
print(f.read())

Example2

fin = open("relativity", "rt")
poem = fin.read()
fin.close()
with open('lines.txt', 'r') as f: 
    contents = f.read()
    print(contents)
poem = ''
fin = open("relativity", "rt")
chunk = 100
while True: 
  fragment = fin.read(chunk)
  if not fragment: 
    break
  poem += fragment
fin.close()
len(poem)
# Output
# # 150)

Readlines Method

Syntax

f.readlines()
with open('lines.txt', 'r') as f: 
    lines = f.readlines()
    print(lines)
    # Output
    # ['On your marks.\n', 'Get set.\n'.'Go!\n']

Write a Test File with write()

File object iterator

File object iterator Example

# using the file object as an iterator
with open('lines.txt', 'r') as f: 
    for line in f:
        # we iterate though the file object using a for loop
        # each iteration the variable line will be set to a line from lines.txt
        print(line, end='')
        # file pointer has moved to a next line.

=> page 263

From the book [O’Reilly, Introducing Python]

flat files


  Strings   Variables   Lists   Tuples   Dictionary  
  Control   Function   Files   Exceptions      
  OOP   Algorithm   Data Structure   back