Source Code Snippets of "Files.py"

Mike Dinder

The entire contents of the "Files.py" file as code examples with syntax highlighting

Auto Generated Using Generate_HTML.py script to auto format each of these pages in a standardized template.

On smaller viewports/windows, please scroll or use your touch/finger to scroll left to right to view all the code displayed.

Files Python Home
                                        # Various File Operations
"""
pystatx - Requires "python3 -m pip install pystatx" and/or activating Virtual Environment First.
Command: source practice_env/bin/activate
"""
import collections
import pprint
# import statx
import time

# from datetime import datetime
from os import listdir, path
from os.path import isfile, join


print('--------------------------------------------------')
# Show Current Filename
print("Show Current Filename and Path of Script that is running.")

print("Current Filename and Path:", path.realpath(__file__))

print('--------------------------------------------------')
# Check if a File Exists, E.g. Math.py (Files in Current Directory)
print("Does File Exist?")

check_file = input("Enter the Filename: ")

print(f"Does The File {check_file} Exist:", path.isfile(check_file))

with open(check_file, 'r') as file_info:
    character_count = collections.Counter(file_info.read())
    value = pprint.pformat(character_count)

    print('A Count of Every Unique Character Found in this File...')
    print(value)

print('--------------------------------------------------')
# List all files found in a directory, with their file extensions on a separate line.
# E.g.Directory = /home/mike/Python_Practice
print("List all Files in a Directory.")
directory = input("Please enter the directory you would like to list: ")

files_list = [f for f in listdir(directory) if isfile(join(directory, f))]

for file in files_list:
    print('***********************************************')
    print("Filename:", file)

    filename_list = file.split('.')
    # stx = statx.statx(file).btime
    # dt_object = datetime.fromtimestamp(stx)
    # formatted_date = dt_object.strftime("%a %b %d %H:%M:%S %Y")

    print('File Extension: ".' + filename_list[-1] + '"')
    print('File Last Accessed Time:', time.ctime(path.getatime(file)))
    # print(f"File Created Time: {formatted_date}")
    print('File Changed Time:', time.ctime(path.getctime(file)))
    print('File Last Modified Time:', time.ctime(path.getmtime(file)))

print('--------------------------------------------------')


                                        

Runs the entire script above. Open your Console Window in Inspect Code to view the output of the script above.

A prompt popup will ask you for various input() commands.

Python in Your Browser

Client-side execution via Pyodide · No server · Full stdlib · numpy, pandas, matplotlib available via micropip

Mobile users will want to use this method. Copy snippets or the entire script from above. You may also copy this script exactly as it is shown and you can run it on your own machine using the Command-Line Interface (CLI).

Loading Pyodide...
>>>
Enter run Shift+Enter new line ↑↓ history