Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

. Write in Python: Lab Assignment: Write a program that will do the following, in order: 1. Under your home directory, print to the console,

.

Write in Python:

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_.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 ------- ------ 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

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 users home directory

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 19c DBA By Examples Installation And Administration

Authors: Ravinder Gupta

1st Edition

B09FC7TQJ6, 979-8469226970

More Books

Students also viewed these Databases questions

Question

LO6 List the components of job descriptions.

Answered: 1 week ago

Question

LO3 Define job design and identify common approaches to job design.

Answered: 1 week ago