Snippets
Files
read-file
# read_file.py
def read_file(file_path):
# Q: When is the file closed?
#
# The file is automatically closed as soon as the with
# block is exited. This is one of the key benefits of using the
# with statement in Python, as it manages resource allocation
# and de allocation (in this case, opening and closing the file)
# effectively and safely, even in the case of exceptions.
try:
with open(file_path, 'r') as file:
data = file.read()
return data
except FileNotFoundError:
return "File not found."
if __name__ == "__main__":
# echo.eg.lines > /tmp/output.txt
file_path = '/tmp/output.txt' # Replace with your file path
content = read_file(file_path)
print(content)
write-file
# write_file.py
def write_file(file_path, content):
with open(file_path, 'w') as file:
file.write(content)
if __name__ == "__main__":
file_path = '/tmp/output.txt' # Replace with your file path
content = "Hello, this is a test."
write_file(file_path, content)
print(f"Content written to {file_path}")
list-files
import os
import fnmatch
def find_files(directory, extension):
"""
Walks through a directory and finds all files with a given extension.
Parameters:
directory (str): The directory to search in.
extension (str): The file extension to search for.
Returns:
list: A list of paths to the files matching the given extension.
"""
matching_files = []
for root, dirs, files in os.walk(directory):
for file in files:
if fnmatch.fnmatch(file, f"*.{extension}"):
matching_files.append(os.path.join(root, file))
return matching_files
# Example usage
directory_to_search = "/path/to/your/directory"
file_extension = "txt" # Replace with the desired file extension
found_files = find_files(directory_to_search, file_extension)
for file in found_files:
print(file)
GPT explanation
The os.walk(directory)
function in Python is a generator that yields a tuple of three values for each directory in the tree rooted at the directory you pass to it. These three values are:
-
root (
str
): The current directory path it is iterating over. This is the path to the directory from whichos.walk
starts its traversal down into its subdirectories. -
dirs (
list
): A list of names of the subdirectories inroot
(excluding.
and..
if they are present). These are the directories thatos.walk
will explore in future iterations. It's important to note that modifying this list will influence the directories that are walked into byos.walk
. -
files (
list
): A list of names of the non-directory files inroot
. These are the actual file names within the directoryroot
. They are not full paths but just names, so if you need a full path (which is common), you have to join them with theroot
usingos.path.join(root, file)
.
This structure is useful because it allows you to iterate through the directory structure, performing actions on each file and knowing its directory context (from the root
), while also having the ability to see which subdirectories exist in each directory (from dirs
), if you need to perform some action on these or alter the course of the walk.
Threading
thread-pool usage
import time
import concurrent.futures
from datetime import datetime
def sleepy_function(seconds):
start_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"[{start_time}] Sleeping for {seconds} second(s)...")
time.sleep(seconds)
end_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return f"[{end_time}] Done sleeping for {seconds} second(s)"
if __name__ == "__main__":
with concurrent.futures.ThreadPoolExecutor() as executor:
seconds_list = [1, 3, 5, 2] # Different sleep times
results = executor.map(sleepy_function, seconds_list)
for result in results:
print(result)
Templates
template
import os
from string import Template
def verify_file_exists(file_path):
if not os.path.exists(file_path):
raise FileNotFoundError(f"The file {file_path} does not exist.")
def templatize(template_file_path):
verify_file_exists(template_file_path)
with open(template_file_path, 'r') as file:
template_content = file.read()
template = Template(template_content)
# Use os.environ to get the environment variables
output = template.substitute(os.environ)
with open('/tmp/out', 'w') as out_file:
out_file.write(output)
print(output)
''
if __name__ == "__main__":
# Replace 'path_to_your_template_file' with the actual path of your template file
# Requires NAME to be set in the environment
templatize('/Users/nickolaykondratyev/git_repos/my-python-sandbox/data/template_example.txt')
Children