I have this GeoIP address code and when I run this code, it shows error that there is no module named GeoIP. This is my code :
import sys
import GeoIP
gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
with open ('Desktop/trail.txt', 'r') as f:
for line_string in f.readlines():
line = line_string.rstrip()
arr = line.split()
try:
country_code = gi.country_code_by_addr(arr[0])
country_name = "\"" + gi.country_name_by_addr(arr[0]) + "\""
arr.append(country_code)
arr.append(country_name)
except:
arr.append("None")
print ",".join(arr)
This is the error :
line 4, in
gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
GeoIP.error: [Errno 2] No such file or directory: '/usr/local/var/GeoIP/GeoIP.dat'
You forgot to add GeoIP.dat database which is your code using to get information
download it from the official release Here
Scroll down to GeoLite Country and choose Download
Move the downloaded file "GeoIP.dat.gz" to /usr/local/var/GeoIP/
Extract it by right click and select Extract HereOr by the following command: gunzip GeoIP.dat.gz
Then will appear a file named GeoIP.dat leave it in this path
Now you have the database file in path /usr/local/var/GeoIP/GeoIP.dat
try to compile again and let me know if still in problem.
Related
I am trying to update the local aws/config file on my mac. I am not able to read or update any contents of the config file.
import configparser
import os
creds_dir = os.path.dirname("~/.aws/config")
config = configparser.RawConfigParser()
if not config.has_section("some"):
config.add_section("some")
# METHOD -1
config.set("some", 'aws_access_key_id', "access_key")
config.set("some", 'aws_secret_access_key', "secret_key")
config.set("some", 'aws_session_token', "token")
config.set("some", 'aws_security_token', "token")
config.write("~/.aws/config")
# METHOD -2
with open('~/.aws/credentials', 'a') as file_out:
file_out.write("[profile]")
file_out.write("aws_access_key_id = aaa")
file_out.write("aws_secret_access_key = bbb")
file_out.write("aws_session_token = cccc")
I am getting an error:
FileNotFoundError: [Errno 2] No such file or directory: '~/.aws/credentials'
I could open the file from my mac terminal and view it.
Change this to be:
creds_file = os.path.expanduser("~/.aws/config")
or
with open(os.path.expanduser("~/.aws/config"), 'a') as file_out:
The ~ expansion is part of the shell normally and so you need to expand it. expanduser returns a string that has the full path name to the file.
I am a Python newbie and I am having a problem that probably has as a simple answer. I have the following script, which works most of the way, I just get stuck trying the write the output file. The error I get is at the very end: IOError: [Errno 2] No such file or directory: '/D/1_NEW_ANALYSIS/Scripts/Melodic_fsfs/design_Rat01_Run_1.fsf'
Here is the code:
import os
import glob
studydir = 'D:/1_NEW_ANALYSIS'
fsfdir="%s/Scripts/Melodic_fsfs"%(studydir)
templatedir="%s/Scripts/Templates"%(studydir)
subdirs=glob.glob("%s/Subjects/Rat_[0-9][0-9]/Run_[0-2]"%(studydir))
for dir in list(subdirs):
splitdir = dir.split('\\')
# YOU WILL NEED TO EDIT THIS TO GRAB sub001
splitdir_sub = splitdir[1]
subnum=splitdir_sub[-2:]
splitdir_run = splitdir[2]
runnum=splitdir_run[-1:]
print(subnum)
replacements = {'SUBNUM':subnum, 'RUNNUM':runnum}
with open("%s/Melodic_design.fsf"%(templatedir)) as infile:
with open("%s/design_Rat%s_Run_%s.fsf"%(fsfdir, subnum, runnum), 'w') as outfile:
for line in infile:
for src, target in replacements.items():
line = line.replace(src, target)
outfile.write(line)
Anybody have an idea why it doesn't work?
Thanks a lot!
If you are running on windows (I assume you are), studydir should look like:
studydir = 'D:\\1_NEW_ANALYSIS'
so I have a problem trying to run this python code as administrator so I am not able to access and write on host file. Can anyone help me? I have looked through many of other questions but non of them seem to work.
Host File Directory: C:\Windows\System32\Drivers\etc\hosts
(Such as)
Request UAC elevation from within a Python script?
Some of these answers actually work on prompting to get administrator access, but it still doesn't give permission to my program. The only way I figured out is to run python shell as administrator first and then run the code or run the command prompt as administrator and open python file with command prompt.
WEBSITE
https://boostlog.io/#faisalnad/create-a-website-blocker-with-python-5afe86ff47018500491f4898
This program is made for blocking website.
import time
from datetime import datetime as dt
# change hosts path according to your OS
hosts_path = r”C:\Windows\System32\Drivers\etc\hosts”
# localhost's IP
redirect = "127.0.0.1"
# websites That you want to block
website_list = ["www.facebook.com","facebook.com",
"dub119.mail.live.com","www.dub119.mail.live.com",
"www.gmail.com","gmail.com"]
while True:
# time of your work
if dt(dt.now().year, dt.now().month, dt.now().day,8) < dt.now() < dt(dt.now().year, dt.now().month, dt.now().day,16):
print("Working hours...")
with open(hosts_path, 'r+') as file:
content = file.read()
for website in website_list:
if website in content:
pass
else:
# mapping hostnames to your localhost IP address
file.write(redirect + " " + website + "\n")
else:
with open(hosts_path, 'r+') as file:
content=file.readlines()
file.seek(0)
for line in content:
if not any(website in line for website in website_list):
file.write(line)
# removing hostnmes from host file
file.truncate()
print("Fun hours...")
time.sleep(5)
This is the error:
Working hours...
Traceback (most recent call last):
File "C:\Users\Key\Desktop\random project.py", line 19, in <module>
with open(hosts_path, 'r+') as file:
PermissionError: [Errno 13] Permission denied: 'C:\\Windows\\System32\\Drivers\\etc\\hosts'
FILE DIRECTORY
You can add write permission for the user under which your program runs following this link to add permission to the host file
I am a complete beginner with Python. I literally started last weekend. I am using Python 3.
I am trying to read text from a pdf file. I first tried pyPDF2 following the instructions in Automate the Boring Stuff, but the result I got had no spaces between words and was therefore unusable. I then installed pdfminer3k by typing "pip install pdfminer3k" in the command line.
I then entered the following lines into the interpreter:
import pdfminer, os
base_path = ("C://Users//ross_")
my_file = os.path.join(base_path + "/" + "sample2.pdf")
log_file = os.path.join(base_path + "/" + "pdf_log.txt")
password = ""
extracted_text = ""
fp = open(my_file, "rb")
parser = PDFParser(fp)
document = PDFDocument(parser, password)
But the last line gave me this error message:
Traceback (most recent call last):
File "", line 1, in
document = PDFDocument(parser, password)
NameError: name 'PDFDocument' is not defined
Does anyone have an idea why I get that error message? I thought PDFDocument would have been defined in the pdfminer module. More generally, how do figure out stuff like this? Isn't there a resource somewhere that explains how to use modules like pdfminer? Many thanks and apologies for my total ignorance.
I want to print a pdf file in python. My code is as below:
def printing_reports():
import os
fp = open("/path-to-file/path.txt",'r')
for line in fp:
os.system('lp -d HPLaserJ %s' % (str(line)))
I am on Fedora 20. path.txt is a file that contain path to the pdf file like '/home/user/a.pdf'
When I run the code it says no such file or directory.
Thanks
Try this code may help:
import os
def printing_reports():
fp = open("/path-to-file/path.txt",'r')
for line in fp:
os.system('lp -d HPLaserJ {0}'.format(line.strip()))
printing_reports()
Make sure the file in every line exists.
Old question, but as I needed a an answer of how to print a pdf file from python, I found this answer more profound:
import cups
conn = cups.Connection()
printers = conn.getPrinters()
printer_name = printers.keys()[0]
ppd_options = {}
cups_job_id = conn.printFile(printer_name,'/path/to/a.pdf',"Title printjob", ppd_connection_options)
It uses the pycups module, which needs CUPS >= 1.7 installed on your system (according to their GitHub page)
The ppd_options dictionary might just be empty. (PPD - Postscript Printer Driver)