Created for
os.getcwd()
>>> os.getcwd()
'/data/python_demos/music/The Cure'
os.listdir(path='dirname')
>>> os.listdir()
['Seventeen Seconds', 'Disintegration']
os.chdir(path)
>>> os.chdir("Seventeen Seconds")
>>> os.getcwd()
'/data/python_demos/music/The Cure/Seventeen Seconds'
os.mkdir(path)
>>> os.listdir()
['Seventeen Seconds', 'Disintegration']
>>> os.mkdir("lyrics")
>>> os.listdir()
['lyrics', 'Seventeen Seconds', 'Disintegration']
os.makedirs(path)
>>> os.listdir("The Cure")
['lyrics', 'Seventeen Seconds', 'Disintegration']
>>> os.makedirs("The Cure/test/subtest")
>>> os.listdir("The Cure")
['test', 'lyrics', 'Seventeen Seconds', 'Disintegration']
>>> os.listdir("The Cure/test/")
['subtest']
os.rename(src, dst)
os.listdir("The Cure")
['test', 'lyrics', 'Seventeen Seconds', 'Disintegration']
>>> os.rename("The Cure/test", "The Cure/TEST")
>>> os.listdir("The Cure")
['TEST', 'lyrics', 'Seventeen Seconds', 'Disintegration']
os.rmdir(path)
>>> os.listdir("The Cure/TEST/")
['subtest']
>>> os.rmdir("The Cure/TEST/subtest/")
>>> os.listdir("The Cure/TEST/")
[]
file
object and its methods and built-in functionsfh = open(file_path, mode="mode")
fh.read()
contents =f.read()
fh.write(str)
fh.writelines(sequence)
os.remove(file_path)
width
statement
with expression as identifier :
statement
statement
...
statement
width
statement
with open("test.txt") as fh:
data = fh.read()
do something with data
The tasks bellow needs some test data.
You can download the Task_and_HW.zip (rename it to Task_and_HW.zip
and extract) in order to get the data and the pre-given folder structure.
Create a simple backup function to archive the content in one folder (src) int oother folder (dest), while setting a timestamp into filenames, as explained in the function docstring below
def backup(src, dest):
"""Backup files in src folder into dest folder.
Do not remove the files in source folder.
To each file attach suffix with curent timestamp in the form
'2018-04-12_18-30-45'
Args:
src (string): Source folder
dest (string): Destination folder
Example:
/src/track5.mp3 => /dest/track5.mp3_2018-04-12_18-30-45
"""
def get_timestamp():
#get the current local date-time
cldt = datetime.datetime.today()
# get the timestamp as a string with given format
timestamp = datetime.datetime.strftime(cldt, '%Y-%m-%d_%H_%M_%S')
return timestamp
Note, that the solution, presented here is for exercise purpose. You do not want to use it in production code! Later, we will discuss the safer technique.
Distribute files in one folder (src) into a corresponding sub-folders, according to their file-type, as explained in the function docstring bellow
def cataloger(src):
"""Distribute files in the src folder into a corresponding sub-folders,
according to their file-type.
Each catalogue, is a subfolder made by a filename extension
Examples:
/data/notes1.txt => /data/txt/notes1.txt
/data/notes2.txt => /data/txt/notes2.txt
/data/picture1.png => /data/png/picture1.png
/data/track1.mp3 => /data/mp3/track1.mp3
Args:
src (string): source folder
"""
os.path.splitext()
method from the os.path module
import os.path
filename = "/data/new/test.txt"
extension = os.path.splitext(filename)[1][1:]
print(extension)
These slides are based on
customised version of
framework