Add and delete URLs

Once we create queue and crawled files, we need to add data to them. We can do this by using a function that accepts two arguments:

  • the path to the file.
  • the data that need to be added to the file.

Here is the code you need to add to the file:

def append_to_file(path, data):
    with open(path, 'a') as file: # opens the file in the append mode.
        file.write(data + '\n') # adds data (links) to file and bumps the cursor on the new line.

We will also create a function that will delete (overwrite) a file, which we will use in later sections:

def delete_file_contents(path):
    with open(path, 'w'): # opens data in write mode and overwrite it

The pass statement used above ensures that the block has at least one statement, but does nothing.

Geek University 2022