My QA Projects

QA Projects I was involded.

View on GitHub

Writing Text Files

Getting a File object

with open(filename, 'w') as f:

Write Method

Syntax

f.write(s)

Write Mothod Example

with open('out.txt', 'w') as f: 
    f.write("Hello, world!")
    f.write("Good morning!\n")
    f.write("Good night!\n")

Writelines Method

Syntax

f.writelines(sequence)

Writelines Example

with open('out.txt', 'w') as f: 
    lines = [f'line{n}' for n in range(1,6)]
    # list comprehension to generate a list with string items: each item will be the string line 1 to line 5
    f.writelines(lines)
    # there is no new line character, therefore all in one line
with open('out.txt', 'w') as f: 
    lines = [f'line{n}\n' for n in range(1,6)]
    f.writelines(lines)

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