[Solved] Python script looking for nonexistent file

Check the root variable. You might be looking for the file at ./access_log-20150215 that is actually in a subdirectory such as ./subdir/access_log-20150215. If you want to explore all subdirectories use f=open(os.path.join(root,file),’r’) but if you only want the top directory you can use os.listdir(‘.’) 3 solved Python script looking for nonexistent file

[Solved] ‘NoneType’ object has no attribute ‘__getitem__’ when I try to return a dict

I don’t see any return statement in the chossen_old_words() function so while you build a dictionary in the function (I suppose it’s the control one), you don’t return it. When you accept the return value in the next_word() function, as I said the chossen_old_words() function returns nothing. However, in Python every function returns something and … Read more

[Solved] extract contact information from html with python

I use this code to extract information # _*_ coding:utf-8 _*_ import urllib2 import urllib import re from bs4 import BeautifulSoup import sys reload(sys) sys.setdefaultencoding(‘utf-8′) def grabHref(url,localfile): html = urllib2.urlopen(url).read() html = unicode(html,’gb2312′,’ignore’).encode(‘utf-8′,’ignore’) soup = BeautifulSoup(html) myfile = open(localfile,’wb’) for link in soup.select(“div > a[href^=http://www.karmaloop.com/kazbah/browse]”): for item in BeautifulSoup(urllib2.urlopen(link[‘href’]).read()).select(“div > a[href^=mailto]”): contactInfo = item.get_text() print … Read more

[Solved] How do I display the max function i created in python

The below code does exactly what you want. def getInt(prompt): try: n = int(input(prompt)) return n except ValueError: print(“I was expecting a number, please try again…”) getInt() def maxNum(lst): if not lst: # if list is empty return None max_elem = lst[0] for x in lst: if x > max_elem: max_elem = x return max_elem … Read more

[Solved] How to populate Objects in a an existing file [closed]

class People: def __init__(self, fname=None, lname=None, age=None, gender=None): self.fname = fname self.lname = lname self.age = age self.gender = gender def display(self): print self.fname people = [People(‘John’,’W Cooper’,23,’Male’), People(‘James’,’W Cooper’,30,’Male’), People(‘Kate’,’W Cooper’,20,’Female’)] f = open(“abc.txt”, “w”) for person in people: f.write( person.fname +”,”+ person.lname +”,”+ str(person.age) +”,”+ person.gender + ‘\n’ ) person.display() f.close() 5 solved … Read more

[Solved] Python While loop not ending when variable changes [closed]

I read you say that your variable is changing, but where? Two things: you’ve got to change the variable you’re checking AND you have to change that condition test, that while loop condition is True when the variable var[5] is different than ‘left’ or different that ‘right’ (if it’s “left”, then it’s different than ‘right’ … Read more

[Solved] OCR – How to recognize numbers inside square boxes using python?

the code below for me is doing decent job but it’s hyper parameter sensitive : import cv2 import imutils import numpy as np import matplotlib.pyplot as plt from matplotlib import pyplot as plt def square_number_box_denoiser(image_path=”/content/9.png”,is_resize = False, resize_width = 768): ”’ ref : https://pretagteam.com/question/removing-horizontal-lines-in-image-opencv-python-matplotlib Args : image_path (str) : path of the image containing numbers/digits … Read more

[Solved] Python: adding results in for loop

move the total=0 out side the loop prices = { “banana” : 4, “apple” : 2, “orange” : 1.5, “pear” : 3, } stock = { “banana” : 6, “apple” : 0, “orange” : 32, “pear” : 15, } total = 0 for key in prices: inventory = (prices[key] * stock[key]) print key print “inventory … Read more

[Solved] How do functions work? [closed]

What you did, you failed to call the function that is why you facing this problem. Use this instead and you can able to get the address of a function. In Python v2.x #!/usr/bin/python def get_address(): address = raw_input(“What is your address: “) return address a = get_address() print a What is raw_input? It ask … Read more

[Solved] List comprehensions

for x in y_train: x.append(element) example: >>> listOfLists = [[1,2], [2,3], [4,5]] >>> for x in listOfLists: … x.append(2) >>> listOfLists [[1, 2, 2], [2, 3, 2], [4, 5, 2]] 0 solved List comprehensions