More python-y stuff.
This commit is contained in:
		
							parent
							
								
									891e3ac0da
								
							
						
					
					
						commit
						091bd47256
					
				
							
								
								
									
										33
									
								
								bank.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								bank.py
									
									
									
									
									
										Normal file
									
								
							| @ -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. | ||||||
							
								
								
									
										44
									
								
								ex15.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								ex15.py
									
									
									
									
									
										Normal file
									
								
							| @ -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. | ||||||
							
								
								
									
										4
									
								
								ex15_sample.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								ex15_sample.txt
									
									
									
									
									
										Normal file
									
								
							| @ -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. | ||||||
							
								
								
									
										33
									
								
								ex16.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								ex16.py
									
									
									
									
									
										Normal file
									
								
							| @ -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. | ||||||
							
								
								
									
										47
									
								
								python-parse-html.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								python-parse-html.py
									
									
									
									
									
										Normal file
									
								
							| @ -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 = """ | ||||||
|  | <html><head><title>The Dormouse's story</title></head> | ||||||
|  | <body> | ||||||
|  | <p class="title"><b>The Dormouse's story</b></p> | ||||||
|  | 
 | ||||||
|  | <p class="story">Once upon a time there were three little sisters; and their names were | ||||||
|  | <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, | ||||||
|  | <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and | ||||||
|  | <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; | ||||||
|  | and they lived at the bottom of a well.</p> | ||||||
|  | 
 | ||||||
|  | <p class="story">...</p> | ||||||
|  | """ | ||||||
|  | 
 | ||||||
|  | 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") | ||||||
							
								
								
									
										47
									
								
								python-web.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								python-web.py
									
									
									
									
									
										Normal file
									
								
							| @ -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 = """ | ||||||
|  | <html><head><title>The Dormouse's story</title></head> | ||||||
|  | <body> | ||||||
|  | <p class="title"><b>The Dormouse's story</b></p> | ||||||
|  | 
 | ||||||
|  | <p class="story">Once upon a time there were three little sisters; and their names were | ||||||
|  | <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, | ||||||
|  | <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and | ||||||
|  | <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; | ||||||
|  | and they lived at the bottom of a well.</p> | ||||||
|  | 
 | ||||||
|  | <p class="story">...</p> | ||||||
|  | """ | ||||||
|  | 
 | ||||||
|  | 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") | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user