2.3 Exercise on OS

2.3.1 Scenario

You are part of an international team working on a project to measure environmental pollution. The ten cities participating in the project are Cairo, Dhaka, Jakarta, Karachi, Manila, Melbourne, Osaka, Shanghai, Singapore and Tokyo.

Your role in the collaboration is to consolidate and organise the data from the various measurements. This zip file contains all the data for the measurements made during the past year. Here are some things to note:

  • The files have been named using the format month-date_city.txt. (e.g. may-10_singapore.txt)
  • The data collection is sporadic. Therefore, there is data only for some days of each month. These dates are different for each city.
  • There are also other administrative files in .pdf, .png or .jpg formats which you should ignore.

2.3.2 What you have to do

  1. Upload the zip file to your Colab instance.

  2. Lets use Python to unzip this file (because we can!). Here is the code:

    import zipfile
    with zipfile.ZipFile('file_name','r') as zf:
        zf.extractall()
  3. We like to convert the filename to a more useful numerical format. Specifically, we want to convert month-date-city.txt. (e.g. may-10-singapore.txt) to a mm-dd-city.txt (e.g. 05-10-singapore.txt) format.

    Using the following list (or otherwise) write a snippet of python code to convert 'oct-08_singapore.txt' to 10-08-singapore.txt. You will need to use your knowledge of strings along with the functions split() and index. Hint: Split the file name so that you can separate the month, date and city.

    ['jan', 'feb', 'mar', 'apr',
    'may', 'jun', 'jul', 'aug',
    'sep', 'oct', 'nov', 'dec']
  4. Incorporate your code into a function named rename_my_file(old_filen_ame) that accepts the old filename as the argument and return the new file name.

  5. Use a for loop to apply the function `rename_my_file to the list of files shown below. Print out your result in the form old-file-name ----> new-file-name

['oct-08_singapore.txt', 'jul-10_cairo.txt', 'may-15_dhaka.txt',
 'may-13_cairo.txt', 'oct-21_cairo.txt', 'jan-10_singapore.txt',
 'jun-20_tokyo.txt', 'aug-06_jakarta.txt', 'dec-21_karachi.txt',
 'jan-01_tokyo.txt']
  1. Use glob to list all the .txt files in the folder original files. Using a for loop along with os.rename function, rename all the files into the new name format.

  2. Use a for loop to create a folder for each of the cities. We have provided the list of cities below for your convenience.

    ['Cairo', 'Dhaka', 'Jakarta', 'Karachi', 'Manila',
     'Melbourne', 'Osaka', 'Shanghai', 'Singapore', 'Tokyo']
  3. Use glob to list all the files from Cairo. Then use a for loop along with shutil.copyfile() to copy all the files from Cairo to the corresponding folder.

  4. Use a for loop to combine all the previous actions to copy all the files to the corresponding folders of the cities.