Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Objectives: Creating files and folders Moving files and folders around the file system Managing file permissions Tasks: > Python provides access to metadata information about
Objectives: Creating files and folders Moving files and folders around the file system Managing file permissions Tasks: > 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: omtime: time of last modification (1s -1) octime: time of last status change (1s -lc) o atime: time of last access (Is -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. IPT 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: 05.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 directoryIPT 2. Under your home directory, create a directory called "Lab3" 1PT 3. Change directory to "Lab3" 4. Print to the console the name of the current directory 1PT 5. Using a loop, create (touch) 10 files with the name of "test_.txt" where is the number from 1 to 10 6. Using a loop, create 5 sub-directories with the name of "subdir_" where 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 <><><><>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started