Objectives:

  • Creating files and folders
  • Moving files and folders around the file system
  • Managing file permissions

Tasks:

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:

  • mtime: time of last modification (ls -l)
  • ctime: time of last status change (ls -lc)
  • atime: time of last access (ls -lu)

To get each of these time parameters from the result object, precede the timestamp with a st_ and this returns the required property.

Hints:

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.

Lab Assignment:

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:

  • getInodeType: This method takes the name of a file/folder as a parameter returns a value indicating the inode type (File or Directory).
  • listEntries: Displays the contents of a directory per requirement 7 above. It takes the name of the directory as parameter
  • createFiles: Takes the directory name and number of files to create and creates the files per requirement 5 above
  • createSubDirectories: Takes the directory name and number of subdirectories to create and creates the subdirectories per requirement 6
  • renameFiles: Uses directory name, current extension of the file, and new extension of files and renames all files based on requirement 8
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.