Below are the Python scripts for the file and directory operations, including detailed comments:
Python Script for File Operations:
```python
# Write a Python script to create a new text file and write a few lines of text to it.
# Import the os module for file operations
import os
# File creation and writing
def create_and_write_file(filename, content):
# Open the file in write mode ('w')
with open(filename, 'w') as file:
# Write the content to the file
file.write(content)
# Specify the file name and content
file_name = 'sample_file.txt'
file_content = "Hello, this is a sample text file.\nThis file is created using Python."
# Call the function to create and write to the file
create_and_write_file(file_name, file_content)
print(f"File '{file_name}' has been created and written to.")
# Write another script that reads from the created file and prints its contents to the console.
# File reading
def read_file_contents(filename):
# Open the file in read mode ('r')
with open(filename, 'r') as file:
# Read and print the file contents
file_contents = file.read()
print(f"Contents of '{filename}':\n{file_contents}")
# Call the function to read and print file contents
read_file_contents(file_name)
```
Python Script for Directory Operations:
```python
# Write a script to create a new directory and verify its creation using Python.
# Import the os module for directory operations
import os
# Directory creation
def create_directory(directory_name):
# Define the path for the new directory
path = os.path.join(os.getcwd(), directory_name)
# Create the directory
os.mkdir(path)
return path
# Specify the directory name
new_directory = 'test_directory'
# Call the function to create the directory
directory_path = create_directory(new_directory)
print(f"Directory '{new_directory}' has been created at: {directory_path}")
# Create another script to list all the files and subdirectories within a specified directory.
# Import the os module for directory listing
import os
# Directory listing
def list_files_and_subdirectories(directory_path):
# List all files and subdirectories in the specified directory
contents = os.listdir(directory_path)
print(f"Contents of directory '{directory_path}':\n{contents}")
# Call the function to list contents of the directory
list_files_and_subdirectories(directory_path)
```
Python Script for Advanced Task (Optional):
```python
# Implement a script that recursively traverses a directory structure and identifies all text files within it.
import os
# Recursive directory traversal
def identify_text_files(directory_path):
text_files = []
for root, dirs, files in os.walk(directory_path):
for file in files:
if file.endswith('.txt'): # Identify text files
text_files.append(os.path.join(root, file))
return text_files
# Specify the directory path for traversal
specified_directory = 'specified_directory'
# Call the function to identify text files within the specified directory
text_files_in_directory = identify_text_files(specified_directory)
print(f"Text files within directory '{specified_directory}':\n{text_files_in_directory}")
```
These scripts demonstrate file and directory operations in Python, utilizing built-in functions from the os module. Each script is thoroughly commented to explain the purpose and functionality of each code block.