Using the file-system

Directory Manipulations

Directory Manipulations

Get the current working directory

os.getcwd()
Return a string representing the current working directory

	    >>> os.getcwd()
	    '/data/python_demos/music/The Cure'
	  

List entries in a directory

os.listdir(path='dirname')
Return a list containing the names of the entries in the directory given by path
listdir() returns both files and folders, with no indication of which is which.
Consider using os.scandir() when you need an iterator, instead of list

	    >>> os.listdir()
	    ['Seventeen Seconds', 'Disintegration']
	  

Change the current working directory

os.chdir(path)
Change the current working directory to the specified path.

	    >>> os.chdir("Seventeen Seconds")
	    >>> os.getcwd()
	    '/data/python_demos/music/The Cure/Seventeen Seconds'
	  

Make directory

os.mkdir(path)
Create a directory with given path name

	    >>> os.listdir()
	    ['Seventeen Seconds', 'Disintegration']

	    >>> os.mkdir("lyrics")

	    >>> os.listdir()
	    ['lyrics', 'Seventeen Seconds', 'Disintegration']
	  

Make directory tree

os.makedirs(path)
Like mkdir(), but makes all intermediate-level directories needed to contain the leaf directory.
Recursive directory creation.

	    >>> 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']
	  

Rename file or directory

os.rename(src, dst)
Rename the source file or directory to given destination

	    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']
	  

Remove directory

os.rmdir(path)
Remove (delete) the directory path.
Only works when the directory is empty, otherwise, OSError is raised.
This is safer than os.removedirs(), which will delete even non-empty directories.

	    >>> os.listdir("The Cure/TEST/")
	    ['subtest']

	    >>> os.rmdir("The Cure/TEST/subtest/")

	    >>> os.listdir("The Cure/TEST/")
	    []
	  

Files Manipulations

Files Manipulations

Overview

For basic files operations, like reading, writing, appending, to files we can use the built-in file object and its methods and built-in functions

open file

fh = open(file_path, mode="mode")
Open file by the given pathname and return a corresponding file object
mode
'r' - open for reading (default)
'w' - open for writing, truncating the file first
'a' - open for writing, appending to the end of the file if it exists
After the work with the file is done, you have to call fh.close() in order to release the fh

Read from file

fh.read()
Reads file content and stores it in variable
fh.readlines()

	    contents =f.read()
	  

Write to a file

fh.write(str)
Writes a string to the file. There is no return value
fh.writelines(sequence)
Writes a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings.

Remove a file

os.remove(file_path)
Removes a file with the given file_path

the with statement


			with expression as identifier :
			    statement
			    statement
			    ...
			    statement
		
Useful to open a file, process its contents, and make sure it is closed

the with statement


			with open("test.txt") as fh:
			    data = fh.read()
			    do something with data
		

Live Demo

Resources

Reading and Writing Files
Python File Methods

Exercises

Prerequisites

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.

Task1: Simple Backup

The Task

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
			  """
		

Hints

To get the current timestamp, you can use next function:

			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
		

Solution

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.

Get the code!

Task2: Simple Cataloger

The Task

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
		    """
		

Hints

To extract the file extension, you can use the 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)
		

Submission

Please, prefix your filenames/archive with your name initials, before sending.
For instance: iep_task1.py or iep_tasks.rar
Send files to progressbg.python.course@gmail.com

These slides are based on

customised version of

Hakimel's reveal.js

framework