Skip to content

Latest commit

 

History

History
62 lines (39 loc) · 3.71 KB

File metadata and controls

62 lines (39 loc) · 3.71 KB

This code provides basic file system operations using Python, such as creating, reading, searching, and exporting files. It simulates a simple file manager, focusing on functionality like working with directories and file contents.

Explanation of Key Functions: ------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1. create_file(filename, data)

Purpose: Creates a file and writes data into it.

How it works: 1.Opens the file in write mode ('w'). If the file doesn't exist, it's created. 2.Writes the string data to the file. 3.Prints confirmation or error if something goes wrong.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2. read_file(filename)

Purpose: Reads and displays the content of a file.

How it works: 1.Opens the file in read mode ('r'). 2.Reads the content and prints it. 3.Handles errors if the file doesn't exist.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3. search_files(directory, keyword)

Purpose: Lists all files in a directory and filters them by a given keyword.

How it works: 1.Uses os.listdir(directory) to retrieve all files in the directory. 2.Filters the files using list comprehension (if keyword in f). 3.Prints the matching files or error if the directory is not found.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

4. search_specific_file(directory, target_file)

Purpose: Checks if a specific file exists in a directory.

How it works: 1.Retrieves the list of files in the directory using os.listdir(). 2.Checks if target_file is in the list. 3.Prints and returns whether the file was found.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

5. export_file(filename, target_directory)

Purpose: Copies the content of a file to another directory.

How it works: 1.Ensures the target directory exists. If not, creates it using os.mkdir(). 2.Reads the source file and writes its content to a new file in the target directory. 3.Handles errors if the file or directory is not found.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Resources and websites:

1.https://www.chatgpt.com/ 2.https://www.perplexity.ai/