A function to create a single directory, returns a directory object. | |
A function to create a multiple directories, returns a multiple nested directories. | |
open() returns a file object, and is most commonly used with two arguments: open(filename, mode) . The first argument is a string containing the filename. The second argument is another string containing a few characters describing the way in which the file will be used. mode can be ‘r’ when the file will only be read, ‘w’ for only writing (an existing file with the same name will be erased), and ‘a’ opens the file for appending; any data written to the file is automatically added to the end. ‘r+’ opens the file for both reading and writing. The mode argument is optional; ‘r’ will be assumed if it’s omitted. |
os
is a standard library that offers capabilities of creading directories. Following is how to achieve it:
mkdir()
method creates a directory, try to create a directory python_basics
:
import os
# Create a directory named python_basics
os.mkdir('python_basics')
Likewise,
makedirs()
creates directories, ‘/’ follows after each directory name:
# Create directories
# Following code create day01 and day02 under directory python_basics
os.makedirs('python_basics/day01')
os.makedirs('python_basics/day02')
open(filename, mode)
creates a file object. Look at Key syntax introduction for more details.
Create a file with open()
function.
# Let's create a text file
text_file = open('file.txt', 'w')
How to create a file under a directory?
There are two ways to read/write files:
# Normal approach to write and must close the file with `close()` method.
text_file.write('Happy coding')
text_file.close()
# With a `with` keyword
with open('file.txt', 'w') as text_file:
text_file.write('Python is a great programming language')
What is the difference with or without using the with
keyword?
It is good practice to use the with keyword when dealing with file objects. The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point. Using with is also much shorter than writing equivalent try-finally blocks:
If you’re not using the with keyword, then you should call f.close() to close the file and immediately free up any system resources used by it. If you don’t explicitly close a file, Python’s garbage collector will eventually destroy the object and close the open file for you, but the file may stay open for a while. Another risk is that different Python implementations will do this clean-up at different times.
# Read them all together:
with open('file.txt', 'r') as file:
print(file.read())
# Loop through line by line
file = open('file.txt', 'r')
for each_line in file:
print(each_line)
file.close()
# Read with `readline()` method
with open('file.txt', 'r') as file:
print(file.readline())
Write a function to create a turtle_practice.py
file with the following code:
from turtle import *
def draw_something(sides):
speed(100)
for each_shape in range(12):
for each_line in range(sides):
forward(100)
right(360/sides)
right(360/12)
done()
draw_something(6)
Homework: Create a file hierarchy
Write function(s) to create directories and files to achiece the following file hierarchy in the picture.
Input
import os
# Implement the function
def createfile(filename):
# Implement the funtion
def createdir(dirname):
# Implement the function
def createdirs(dirsname):
# Implement the function
def create_hierarchy():
Output