🌿

Misc Q&A

Hows

How do you list all the files in a directory using python?
  • os.listdir() will get you everything that's in a directory - files and directories.
  • Read More
How do you get all text in p tags from an html file?
from bs4 import BeautifulSoup

with open("file.extension", "r") as f:
		contents = f.read()
		soup = BeautifulSoup(contents, 'lxml')

p_tags = soup.find_all("p");
p_tags.get_text()

arr = []

for p_tag in p_tags:
		arr.append(p_tag.get_text())

import os, sys

fd = os.open('CleanCode1.md', os.O_RDWR|os.O_CREAT)

os.write(fd, str.encode("\n".join(arr)))

os.close(os.open('CleanCode1.md', os.O_RDWR))

with open('CleanCode1.md') as f:
		f.read_lines()
  • Read More
How do you get history from Python ide?
import readline
readline.write_history_file("history.py")
  • Read More
How do you read and rename file names to remove whitespace in python?
import os
l = os.readdir()
new_names = [re.sub('\s+','',el) for el in l]
  • Read More
  • Practice
  • // How do you read and rename files to remove whitespace in python? 
    
    import os, re
    
    files = os.readdir()
    
    for file_name in files:
    	stripped_file_name = re.sub('\s+', '', file_name)
    	os.rename(file_name, stripped_file_name)
Difference betweopen(file_name, 'rb')en open(file_name, 'rb') and open(file_name, 'r')?
  • Stands for read buffer and read. read buffer will return string literals, and read will return plain strings
  • Read More
When using emmet, what is the notation for custom HTML tags?
p[title="Hello world"]
  • Read More

Whats

What is a class variable?
  • Class variables are variables that are shared with all instances (objects)
class SomeClass:
    variable_1 = “This is a class variable”
    variable_2 = 100   #this is also a class variable
What is a namespace in python?
  • It is synonymous with scope
  • E.g. local scope vs local namespace
image
  • Read More
What does XHR stand for?

It is short for XML HTTP Request