diff --git a/YO5.txt b/YO5.txt new file mode 100644 index 0000000..b5ba9e9 --- /dev/null +++ b/YO5.txt @@ -0,0 +1,3 @@ +yo +c +h diff --git a/YO6.txt b/YO6.txt new file mode 100644 index 0000000..0bbf108 --- /dev/null +++ b/YO6.txt @@ -0,0 +1,3 @@ +jhklj +lkjlk +ljlk diff --git a/bank.py b/bank.py new file mode 100644 index 0000000..ebc23d9 --- /dev/null +++ b/bank.py @@ -0,0 +1,33 @@ +from sys import argv + +script, filename = argv + +print(f"We're going to erase {filename}.") +print("If you don't want that, hit CTRL-C (^C).") +print("If you do want that, hit RETURN.") + +input("?") + +print("Opening the file....") +target = open(filename, 'w') + +print("Truncating the file. Goodbye!") +#target.truncate() + +print("Now I'm going to ask you for three lines.") +line1 = input("line 1: ") +line2 = input("line 2: ") +line3 = input("line 3: ") + +print("I'm going to write these to the file.") + +target.write(line1 + "\n" + line2 + "\n" + line3 + "\n") + +print("And finally, we close it.") +target.close() + +text = open(filename) +print(text.read()) + +# 4. You are specificing writing to a file as opposed to reading from it. +# 5. You don't need it, 'w' overwrites the file. diff --git a/ex15.py b/ex15.py new file mode 100644 index 0000000..7d886ab --- /dev/null +++ b/ex15.py @@ -0,0 +1,44 @@ + # Import the argv tool +from sys import argv +import os.path + +# Grab two arguments wtih argv +script, filename = argv + +if not os.path.isfile(filename): # If file doesn't exist, create it. + with open(filename, 'w') as f: # Write the following string to it. + print(" NEW FILE!\n ONLY 99 CENTS!\n Just kidding, it's free.\n Welcome to the galaxy.", file=f) + + #Close the file. + filename.close() + +# set the variable txt to the open command for the file taken as input. + +txt = open(filename) +#txt.close() #Break stuff + +# Tells user that their file of the filename {filename} is going to be printed. +print(f"Here's your file {filename}:") + +# Print file +print(txt.read()) + +# Close the file +txt.close() + +# Asks them to input the name again. +print("Type the filename again:") + +# Take input on a prompt. +file_again = input("> ") + +# Take input on a prompt. +txt_again = open(file_again) + +# Read the file and print. +print(txt_again.read()) + +txt.close() + +#1-4 Done. +#5. Getting through argv lets you run operations on it throughout the script without having to wait for input. diff --git a/ex15_sample.txt b/ex15_sample.txt new file mode 100644 index 0000000..e661228 --- /dev/null +++ b/ex15_sample.txt @@ -0,0 +1,4 @@ +This is stuff I typed into a file. +A new universe. +It is really cool stuff. +Lots and lots of fun to have in here. diff --git a/ex16.py b/ex16.py new file mode 100644 index 0000000..ebc23d9 --- /dev/null +++ b/ex16.py @@ -0,0 +1,33 @@ +from sys import argv + +script, filename = argv + +print(f"We're going to erase {filename}.") +print("If you don't want that, hit CTRL-C (^C).") +print("If you do want that, hit RETURN.") + +input("?") + +print("Opening the file....") +target = open(filename, 'w') + +print("Truncating the file. Goodbye!") +#target.truncate() + +print("Now I'm going to ask you for three lines.") +line1 = input("line 1: ") +line2 = input("line 2: ") +line3 = input("line 3: ") + +print("I'm going to write these to the file.") + +target.write(line1 + "\n" + line2 + "\n" + line3 + "\n") + +print("And finally, we close it.") +target.close() + +text = open(filename) +print(text.read()) + +# 4. You are specificing writing to a file as opposed to reading from it. +# 5. You don't need it, 'w' overwrites the file. diff --git a/python-parse-html.py b/python-parse-html.py new file mode 100644 index 0000000..41699ec --- /dev/null +++ b/python-parse-html.py @@ -0,0 +1,47 @@ +# import SimpleHTTPServer +# import SocketServer +# +# PORT = 8000 +# +# Handler = SimpleHTTPServer.SimpleHTTPRequestHandler +# +# httpd = SocketServer.TCPServer(("", PORT), Handler) +# +# print("serving at port", PORT) +# httpd.serve_forever() + +from bs4 import BeautifulSoup +# import urllib.request +# +# url = "https://google.com/" +# +# yo = urllib.request.urlopen(url) +# soup = BeautifulSoup(url.read(), 'html.parser') +# print(soup) + +html_doc = """ +The Dormouse's story + +

The Dormouse's story

+ +

Once upon a time there were three little sisters; and their names were +Elsie, +Lacie and +Tillie; +and they lived at the bottom of a well.

+ +

...

+""" + +soup = BeautifulSoup(html_doc, 'html.parser') +print(soup.prettify()) +print("\n") +print(str(soup.title) + "\n") +print(str(soup.title.name) + "\n") +print(str(soup.title.string) + "\n") +print(str(soup.title.parent.name) + "\n") +print(str(soup.p) + "\n") +print(str(soup.p['class']) + "\n") +print(str(soup.a) + "\n") +print(str(soup.find_all('a')) + "\n") +print(str(soup.find(id="link3")) + "\n") diff --git a/python-web.py b/python-web.py new file mode 100644 index 0000000..41699ec --- /dev/null +++ b/python-web.py @@ -0,0 +1,47 @@ +# import SimpleHTTPServer +# import SocketServer +# +# PORT = 8000 +# +# Handler = SimpleHTTPServer.SimpleHTTPRequestHandler +# +# httpd = SocketServer.TCPServer(("", PORT), Handler) +# +# print("serving at port", PORT) +# httpd.serve_forever() + +from bs4 import BeautifulSoup +# import urllib.request +# +# url = "https://google.com/" +# +# yo = urllib.request.urlopen(url) +# soup = BeautifulSoup(url.read(), 'html.parser') +# print(soup) + +html_doc = """ +The Dormouse's story + +

The Dormouse's story

+ +

Once upon a time there were three little sisters; and their names were +Elsie, +Lacie and +Tillie; +and they lived at the bottom of a well.

+ +

...

+""" + +soup = BeautifulSoup(html_doc, 'html.parser') +print(soup.prettify()) +print("\n") +print(str(soup.title) + "\n") +print(str(soup.title.name) + "\n") +print(str(soup.title.string) + "\n") +print(str(soup.title.parent.name) + "\n") +print(str(soup.p) + "\n") +print(str(soup.p['class']) + "\n") +print(str(soup.a) + "\n") +print(str(soup.find_all('a')) + "\n") +print(str(soup.find(id="link3")) + "\n")