186k views
3 votes
Write a Python or a C program that takes as arguments the type

of operation , the extension of files ,
the directory to operate on . and when
appropriate,

User Rpg
by
8.2k points

1 Answer

5 votes

Final answer:

To write a Python or C program that takes arguments for the type of operation, file extension, and directory to operate on, use programming concepts and libraries. In Python, use the os module, and in C, use standard library functions. Iterate through files in the directory, check the extension, and perform the specified operation.

Step-by-step explanation:

To write a Python or C program that takes arguments for the type of operation, file extension, and directory to operate on, you can use various programming concepts and libraries. In Python, you can use the os module to navigate directories and operate on files, while in C, you can use standard library functions for file handling. Here's an example in Python:

import os
def operate_on_files(operation, extension, directory):
for filename in os.listdir(directory):
if filename.endswith(extension):
if operation == 'delete':
os.remove(os.path.join(directory, filename))
elif operation == 'copy':
os.rename(os.path.join(directory, filename), os.path.join(directory, 'copy_' + filename))
def main():
operation = input('Enter the operation (delete or copy): ')
extension = input('Enter the file extension: ')
directory = input('Enter the directory path: ')
operate_on_files(operation, extension, directory)
if __name__ == '__main__':
main()

This program takes input from the user for the operation ('delete' or 'copy'), file extension, and directory path. It then iterates through the files in the directory, checks if the file has the specified extension, and performs the specified operation.

User Adirmola
by
7.0k points