2.3 Exercise on OS
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
- 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
.jpgformats which you should ignore.
2.3.2 What you have to do
Upload the zip file to your Colab instance.
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()
We like to convert the filename to a more useful numerical format. Specifically, we want to convert
may-10-singapore.txt) to a
Using the following list (or otherwise) write a snippet of python code to convert
10-08-singapore.txt. You will need to use your knowledge of strings along with the functions
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']
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.
forloop to apply the function `
rename_my_fileto 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']
globto list all the
.txtfiles in the folder original files. Using a
forloop along with
os.renamefunction, rename all the files into the new name format.
forloop 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']
globto list all the files from Cairo. Then use a
forloop along with
shutil.copyfile()to copy all the files from Cairo to the corresponding folder.
forloop to combine all the previous actions to copy all the files to the corresponding folders of the cities.