OS Island
Our next destination is OS
Island!
So, my handy guidebook tells me that os
provides many useful methods to work with directories and files. My First Mate would be well pleased! Will definitely make life easier.
Now, we actually have not one, but TWO treasure maps this time!
Remember, the answers can all be found on the maps.
Now, let’s explore OS
Island!
Ok, let’s get organised. First things first – I assume you still have your interactive Python interpreter running?
>>> import os
Mission #1: Where am I?
First, we should try to figure out our current location. Try to find out where your current working directory is.
Mission #2: Check your inventory
And now let’s make sure we have everything we need in our backpack before we go out exploring. Can you figure out how to get a list of all the files in your current directory?
Mission #3: We need a new backpack
Seems like we need a new backpack. Can you figure out how to create a new directory called backpack/
in your current directory using Python? (You can delete this later once we’re out of this island!)
Mission #4: Do we have a new backpack?
Hmmm.. has the backpack been created? Can just double check that the new directory backpack/
now actually exists (with Python)? I do not want to browse through that long list of files in your directory. I want a straight answer from Python - does the directory exist? Yes or No?
Mission #5: We need a new backpack inside a new suitcase
Seems like a single backpack is not enough. We need a new suitcase, and we need another new backpack inside the suitcase. Can you please figure out how to create a new directory called suitcase/backpack/
in your current directory using Python, in a single line? I do not want to be creating suitcase
and suitcase/backpack/
separately. I want it done in one go – time is of the essence!
Mission #6: Hmm… backpack sounds like a silly name.
I think I prefer “rucksack”. Can you please rename backpack/
to rucksack/
using Python?
Mission #7: Which is that heavy item?
Hmm… something is the current directory is quite heavy. Can you pick any one of your files in your current directory, and try to find out the size of the file in bytes? (I actually know two ways to do this!)
Mission #8: Can the rucksack carry items?
Can you make sure that the rucksack can actually carry things? Please check whether rucksack/
is actually a directory (and not a file).
Mission #9: Joining rucksacks and items
Now, seems like I need to join the directories suitcase
, backpack
and a file in the backpack called bottle.txt
together as a single string. But different islands join directory and file names differently. Linux uses forward-slash (suitcase/backpack/bottle.txt
), while Windows uses backslash (suitcase\backpack\bottle.txt
). Can they not just agree on one? Is there any way I can get Python to join the names with the corect separator without me having to worry about which island I am on?
(If you are on Windows, you will see double backslashes in your file name (suitcase\\backpack\\bottle.txt
). This is because \
is an escape indicator in a string, so you need \\
to represent a backslash)
Mission #10: I only want my backpack.
I assume that you have already joined your string from Mission #9. Now I actually only need the directory name (i.e. suitcase/backpack
or suitcase\\backpack
depending on which island you are from). There is no need for bottle.txt
to be shown. Is there any easy way to retrieve only the directory name?
Mission #11: I need both my backpack and my bottle.
Now, I need to separate my bottle from my backpack so that I can drink from my bottle. Can you figure out how to separate suitcase/backpack/bottle.txt
into suitcase/backpack
and bottle.txt
? (Backslash for those from Windows!)
Mission #12: Now, who needs an extension?
I don’t think that .txt
extension after my bottle is useful. Any easy way I can remove that extension so that I have suitcase/backpack/bottle
and .txt
separated? (Backslash for those from Windows!)
And excellent work, my young crew member! You are very capable. I may promote you to First Mate if you keep this up!
Let us now sail to our third and final island!