# The Way of the Program - Fall 2025
#
# Monday, September 29 (week 5)
#
# Introduction to file processing
#
#-------------------------------------------------------------------
# How to tell Python where to look for files

import os

# os.chdir sets the working directory to the specified location

# Example (on a Mac):
# os.chdir("/Users/YOUR_USERNAME/Documents/introcs")

# Example (on Windows):
# os.chdir("C:\\Users\\YOUR_USERNAME\\Documents\\introcs")

# os.getcwd() returns the current working directory as a string
print("Current working directory:")
print(os.getcwd())

#-------------------------------------------------------------------
# Example of string formatting

def tempTable():
    print(str.format("{:>4} {:>7}", "F", "C"))
    fahrenheit = 0
    for fahrenheit in range(0, 121, 10):
        celsius = 5 * (fahrenheit - 32) / 9
        print(f"{fahrenheit:4} {celsius:7.1f}")
        
# See string-formatting-examples.pdf for more examples

#-------------------------------------------------------------------
# Reading information from text files

# reports information about a text file
def fileinfo():
    filename = input("Enter a filename: ")
    fp = open(filename, "r")
    allChars = fp.read()
    fp.close()
    numChars = len(allChars)
    numLines = allChars.count("\n")
    wordList = allChars.split()
    numWords = len(wordList)
    print(f"{filename} contains {numChars} characters, "
          f"{numWords} words, and {numLines} lines.")
    
# Examples:
#
# >>> fileinfo()
# Enter filename: haunting.txt
# haunting.txt contains 504 characters, 83 words, and 11 lines
#
# >>> fileinfo()
# Enter filename: moby.txt
# moby.txt contains 1187402 characters, 207857 words, and 16899 lines

# reports the number of vowels in a text file
def countvowels():
    filename = input("Enter a filename: ")
    fp = open(filename, "r")
    allChars = fp.read()
    fp.close()
    allChars = allChars.upper()
    num = allChars.count("A")
    print(f"{num:6} A's")
    num = allChars.count("E")
    print(f"{num:6} E's")
    num = allChars.count("I")
    print(f"{num:6} I's")
    num = allChars.count("O")
    print(f"{num:6} O's")
    num = allChars.count("U")
    print(f"{num:6} U's")

#-------------------------------------------------------------------
# Prints information about movie directors stored in directors.txt

def directors():
    fp = open("directors.txt", "r")
    lineList = fp.readlines()
    fp.close()
    directorName = input("Enter director name: ")
    for line in lineList:
        line = line.strip()  # remove trailing newline

        # extract individual data fields
        dataFields = line.split("|")
        title = dataFields[0]
        year = dataFields[1]
        director = dataFields[2]

        # this line does the same thing as the four lines above
        title, year, director = line.split("|")

        if director == directorName:
            print(f"{title} ({year}) directed by {director}")
        else:
            pass

#-------------------------------------------------------------------
# Prints information about stock prices downloaded from
# https://www.wsj.com/market-data/quotes/AAPL/historical-prices

def stocks():
    filename = input("Enter stock data filename: ")
    fp = open(filename, "r")
    lineList = fp.readlines()
    fp.close()
    headerLine = lineList[0]
    dataLines = lineList[1:]
    for line in dataLines:
        line = line.strip()  # remove trailing newline
        date, opening, high, low, closing, vol = line.split(",")
        closingPrice = float(closing)
        print(f"Stock closed at ${closingPrice:.2f} on {date}")
    
# Example:
#
# >>> stocks()
# Enter stock data filename: AAPL.csv
# Stock closed at $255.46 on 09/26/25
# Stock closed at $256.87 on 09/25/25
# Stock closed at $252.31 on 09/24/25
# Stock closed at $254.43 on 09/23/25
# Stock closed at $256.08 on 09/22/25
# Stock closed at $245.50 on 09/19/25
# ...
# Stock closed at $226.78 on 10/02/24
# Stock closed at $226.21 on 10/01/24
# Stock closed at $233.00 on 09/30/24
# Stock closed at $227.79 on 09/27/24

