fb pixel

Introduction to Python

  • Profound Academy

    • Status
      • 1
        Input and Output
      • 2
        Variables and Integer Arithmetic
      • 3
        Conditional Operators
      • 4
        Advanced Conditions - Nesting
      • 5
        Types and Variables
      • 6
        Strings
      • 7
        Lists
      • 8
        For Loops
      • 9
        While Loops
      • 10
        continue, break, while...else
      • 11
        String and List methods
      • 12
        Nested Loops
      • 13
        List Comprehension
      • 14
        Tuples and Sets
      • 15
        dict
      • 16
        Functions
      • 17
        Functions 2
      • 18
        Lambda and higher-order functions
      • 19
        Files

  • Appending to a file

    Recall the several modes which are supported for working with files:
    Mode
    Operation
    Example
    'r'
    Read (the default one)
    with open('document.txt', 'r') as f:
    'w'
    Write
    with open('document.txt', 'w') as f:
    'a'
    Append
    with open('document.txt', 'a') as f:
    'r+'
    Read + Write
    with open('document.txt', 'r+') as f:
    'x'
    Create (error if present)
    with open('document.txt', 'x') as f:
    There are different files present on the computer. You are asked to add some lines to them.
    The input contains 2 lines. The first line is the name of the file. The second one is the content we would like to add to that file.
    The program needs to first add the specified line to the file and then print the whole content of the file.
    Input
    doc.txt (initial content)
    Output
    doc.txt (resulting content)
    doc.txt This is the second line
    This is the first line
    This is the first line This is the second line
    This is the first line This is the second line
     
    To check your solution you need to sign in
    Sign in to continue