Python provides a module that allows developers to access and execute several system-level operating system tasks that relate to files and folders. We will examine how to among other things, determine what the current working directory is, to change directories, get the list of files and directories, rename files and directories. We will be using the os module for this purpose.
Python provides access to metadata information about files directly through the os.stat() method. The result of the stat() method call is an os.stat_result object. This provides a list of attributes that correspond to the results of the os.stat() method call. Remember that UNIX files have three timestamps:
To get each of these time parameters from the result object, precede the timestamp with a st_ and this returns the required property.
Check for Existence of file
To check for existence of a file or folder, you may use the os.path module. This module has exists()method that returns a Boolean value to indicate whether a file/folder exists of not. If the file exists, the method returns a True and False otherwise. The method accepts the path to the file or folder. The path could either be relative or absolute.
Check for existence of File or Directory
To check whether an inode is a file or a directory, use the os.path.isdir() or os.path.isfile() method. The isdir() method returns a True if the inode is a directory and false otherwise. These methods both accept the path to the file or folder.
Creating Files and Folders
There are several methods for creating empty files. The open() method automatically creates a file. For example, open("test.txt", a) opens a new file test.txt for appending. An alternative method for creating a file is to use the os.mknod() function, which accepts the path to the file as a parameter. The function requires root privileges on OSX to execute. A subsequent lecture will discuss all the possible ways of working with files.
To create an empty folder, use the os.makedirs() method. Pass the path as a parameter to the method.
Renaming Files
To rename files, you may choose to use the os.rename() method. The method has the following syntax: os.rename(src, dest) where src is the current name of the file/folder, and dest is the new name for the inode object.
Getting the home directory of the current user
The os module provides the expanduser method which is part of the path sub-module for common path manipulation. As an example, os.path.expanduser("~") expands the initial path component in a given path to the user's home directory.
Write a program that will do the following, in order!:
1. Under your home directory, print to the console, the name of your current directory
2. Under your home directory, create a directory called "Lab3"
3. Change directory to "Lab3"
4. Print to the console the name of the current directory
5. Using a loop, create (touch) 10 files with the name of "test_< n >.txt" where < n > is the number from 1 to 10
6. Using a loop, create 5 sub-directories with the name of "subdir_< n >" where < n > is the number from 1 to 5
7. List all entries within the "Lab3" sub directory in the following format:
Note: Do not print the "." and ".." directories
Name Type
------- ------
< name of file or directory > < file or directory >
8. Rename all files that end with .txt to .dat
9. Repeat step 7
Write the following methods and use them to solve the problems above: