Now that we've started working with files, let's take a moment to learn a little more about how your computer stores your files. So far, we've noticed a few things about files:
temp
or numbers
).txt
or .csv
), which describes the file typeFiles also have a location on your computer.
Files are organized in a tree structure. For example, in our class notes directory we have a structure like:
introduction-to-python
|__ data
|__ restaurant-names.txt
|__ restaurants.txt
|__ notes
|__ A-Introduction_to_iPython_Notebooks.ipynb
|__ B-Numeric_Expressions.ipynb
|__ C1-Variables.ipynb
|__ ...
|__ binder
|__ ...
|__ ...
So, within the directory this notebook would be located at introduction-to-python/notes/L2-folders.ipynb
.
Let's understand this a little better. To do so, we'll import the os
module, a convenient way to work with files from within Python.
import os
Let's use os.getcwd()
to print the current working directory (cwd
).
# Print the working directory
start_directory = os.getcwd()
print(f"We are now in:\n{start_directory}")
You should not be surprised to see that you are sitting in the introduction-to-python/notes/
folder.
What's here? Let's use os.listdir()
to list the files in the directory.
# Let's see what kind of files are in this directory
os.listdir()
# Are there some notebooks about Strings here?
[f for f in os.listdir() if 'String' in f]
# What kinds of .csv or .txt files are there here? I guess none.
[f for f in os.listdir() if f.endswith(".csv") or f.endswith(".txt")]
Let's make a directory for the files we made today using os.makedirs()
.
# Create our directory
new_directory = start_directory + "/practice_files"
os.makedirs(new_directory)
print(f"We are making a new directory called:\n{new_directory}\n")
# Check that it's there
print("Did we make it?")
print('practice_files' in os.listdir())
Let's enter (change to) the directory that we made using os.chdir()
.
# Move to new directory
os.chdir(new_directory) # Change the directory
print(f"Ok. We are now in directory:\n{os.getcwd()}") # Confirm that we're there
# And, we can go back:
os.chdir(start_directory) # Change the directory
print(f"Ok. We are now in directory:\n{os.getcwd()}") # Confirm that we're there
One convenient tool that you can use is relative file paths. For example, so far we have used the full filepaths to navigate:
print(start_directory)
print(new_directory)
However, we can also move using filepaths that are defined relative to our current location. For example, from our position in 01-Introduction_to_Python
:
practice_files/
../
shorthand# Where are we again?
print(f"Ok. We are now in directory: {os.getcwd()}")
# Go down
os.chdir('practice_files/') # Change the directory
print(f"Ok. We are now in directory: {os.getcwd()}") # Confirm that we're there
# Go up
os.chdir('../') # Change the directory
print(f"Ok. We are now in directory: {os.getcwd()}") # Confirm that we're there
# Go up two steps
os.chdir('../../') # Change the directory
print(f"Ok. We are now in directory: {os.getcwd()}") # Confirm that we're there
# Go down into the SQl folder
os.chdir('introduction-to-python/data/') # Change the directory
print(f"Ok. We are now in directory: {os.getcwd()}") # Confirm that we're there
# Move up and then down (i.e. move laterally)
os.chdir('../notes/') # Change the directory
print(f"Ok. We are now in directory: {os.getcwd()}") # Confirm that we're there
We can use os.rename(old_name, new_name)
to rename files.
new_name
.Rename
can also be used to move files; technically, the full syntax is os.rename(old_filepath, new_filepath)
.old_name
, new_name
.For example:
# Let's write a silly file
with open("temp.txt", "w+") as f:
f.write("This is a test file")
print(f"Is 'temp.txt' in the directory? {'temp.txt' in os.listdir()}")
print(f"Is 'temporary.txt' in the directory? {'temporary.txt' in os.listdir()}")
print("\nRenaming now...\n")
os.rename('temp.txt', 'temporary.txt')
print(f"Is 'temp.txt' in the directory? {'temp.txt' in os.listdir()}")
print(f"Is 'temporary.txt' in the directory? {'temporary.txt' in os.listdir()}")
Exercise: Let's write two more files("temp2.txt" and "temp3.txt") and move our temp files from notes
to practice_files
using a loop and relative filepaths. Recall that the names of the files (after our renaming) are:
["temporary.txt", "temp2.txt", "temp3.txt"]
# Your code here
Answer:
# Create files
with open("temp2.txt", "w+") as f:
f.write("This is a test file")
with open("temp3.txt", "w+") as f:
f.write("This is a test file")
# Loop through the files we created
for f in ["temporary.txt", "temp2.txt", "temp3.txt"]:
# Define the old and new filepaths
old_name = f
new_name = "practice_files/" + f
\ # Move the files
os.rename(old_name, new_name)
# Check our answer:
# Go to practice files
os.chdir("practice_files/")
# Confirm that we're there
print("Ok. We are now in directory:")
print(os.getcwd())
# See what's here -> the moved files
print("\nThe contents of this directory are:")
print(os.listdir())
Finally, we can get rid of junk using os.remove()
and os.rmdir()
.
Note:
Let's remove the files we created in this notebook, and then remove the practice_files
directory.
for fname in [ 'temporary.txt','temp2.txt', 'temp3.txt']:
try:
os.remove(fname) # Remove each of our files in "practice_files"
except:
print(f"Couldn't find {fname}")
os.chdir('../') # Navigate up to the parent directory
os.rmdir('practice_files/') # Remove the empty "practice_files" folder
There are many helpful operations you can perform on files with Python, including: downloading them from the internet, unzipping them, etc. We will see some of these operations in the coming notebooks.