Python checking file content with string - python

Hello! I have the following script:
import os
import stat
curDir = os.getcwd()
autorun_signature = [ "[Autorun]",
"Open=regsvr.exe",
"Shellexecute=regsvr.exe",
"Shell\Open\command=regsvr.exe",
"Shell=Open" ]
content = []
def read_signature(file_path):
try:
with open(file_path) as data:
for i in range(0,5):
content.append(data.readline())
except IOError as err:
print("File Error: "+ str(err))
read_signature(os.getcwd()+'/'+'autorun.inf')
if(content==autorun_signature):
print("Equal content")
else:
print("Not equal")
It prints not equal, then I tried this method:
import os
import stat
curDir = os.getcwd()
autorun_signature = "[Autorun]\nOpen=regsvr.exe\nShellexecute=regsvr.exe\nShell\Open\command=regsvr.exe\nShell=Open"
content = ""
def read_signature(file_path):
try:
with open(file_path) as data:
content = data.read()
except IOError as err:
print("File Error: "+ str(err))
read_signature(os.getcwd()+'/'+'autorun.inf')
if(content==autorun_signature):
print("Equal content")
else:
print("Not equal")
It also print not equal!
I want to store the content of autorun.inf file in script and every time i find such file I want to check its content if it is or not, I could not do, can anyone help me?
the content of autorun.inf:
[Autorun]
Open=regsvr.exe
Shellexecute=regsvr.exe
Shell\Open\command=regsvr.exe
Shell=Open

Linebreaks under Windows \r\n are different from Linux's \n.
So try replacing \n with \r\n.

It is probably due to the fact that Windows new lines are \r\n instead of \n.
Also, you should escape the "\", so instead use"\\".

Related

Not getting all possible variables from splitting a web-scraped string

I can't get my program to get every string possible from a split.
Here is one thing I tried:
var2 = "apple banana orange"
for var in var2.split():
#Here I would put what I want to do with the variable, but I put print() to show what happens
print(var)
I got:
applebananaorange
Full Code:
import requests
response = requests.get('https://raw.githubusercontent.com/Charonum/JSCode/main/Files.txt')
responsecontent = str(response.content)
for file in responsecontent.split("\n"):
file = file.replace("b'", "")
file = file.replace("'", "")
file = file.replace(r"\n", "")
if file == "":
pass
else:
print(file)
url = 'https://raw.githubusercontent.com/Charonum/JSCode/main/code/windows/' + file + ""
wget.download(url)
What should I do?
It looks like one of the files in the list is not available. It is good practice to always wrap input/output operations with a try/except to control problems like this. The code below downloads all available files and informs you which files could not be downloaded:
import requests
import wget
from urllib.error import HTTPError
response = requests.get('https://raw.githubusercontent.com/Charonum/JSCode/main/Files.txt')
responsecontent = str(response.content)
for file in responsecontent.split("\\n"):
file = file.replace("b'", "")
file = file.replace("'", "")
file = file.replace(r"\n", "")
if file == "":
pass
else:
url = 'https://raw.githubusercontent.com/Charonum/JSCode/main/code/windows/' + file + ""
print(url)
try:
wget.download(url)
except HTTPError:
print(f"Error 404: {url} not found")
It seems to work for me replacing the for statement with this one:
for file in responsecontent.split("\\n"):
...
Instead of responsecontent = str(response.content) try:
responsecontent = response.text
and then for file in responsecontent.split().

Write output data to csv

I'm writing a short piece of code in python to check the status code of a list of URLS. The steps are
1. read the URL's from a csv file.
2. Check request code
3. Write the status code request into the csv next to the checked URL
The first two steps I've managed to do but I'm stuck with writing the output of the requests into the same csv, next to the urls. Please help.
import urllib.request
import urllib.error
from multiprocessing import Pool
file = open('innovators.csv', 'r', encoding="ISO-8859-1")
urls = file.readlines()
def checkurl(url):
try:
conn = urllib.request.urlopen(url)
except urllib.error.HTTPError as e:
print('HTTPError: {}'.format(e.code) + ', ' + url)
except urllib.error.URLError as e:
print('URLError: {}'.format(e.reason) + ', ' + url)
else:
print('200' + ', ' + url)
if __name__ == "__main__":
p = Pool(processes=1)
result = p.map(checkurl, urls)
with open('innovators.csv', 'w') as f:
for line in file:
url = ''.join(line)
checkurl(urls + "," + checkurl)
The .readlines() operation leaves the file object at the end of file. When you attempt to loop through the lines of file again, without first rewinding it (file.seek(0)) or closing and opening it again (file.close() followed by opening again), there are no lines remaining. Always recommended to use with open(...) as file construct to ensure file is closed when operation is finished.
Additionally, there appears to be an error in your input to checkurl. You have added a list (urls) to a string (",") to a function (checkurl).
You probably meant for this section to read
with open('innovators.csv', 'w') as f:
for line in urls:
url = ''.join(line.replace('\n','')) # readlines leaves linefeed character at end of line
f.write(url + "," + checkurl(url))
The checkurl function should return what you are intending to place into the csv file. You are simply printing to standard output (screen). Thus, replace your checkurl with
def checkurl(url):
try:
conn = urllib.request.urlopen(url)
ret='0'
except urllib.error.HTTPError as e:
ret='HTTPError: {}'.format(e.code)
except urllib.error.URLError as e:
ret='URLError: {}'.format(e.reason)
else:
ret='200'
return ret
or something equivalent to your needs.
Save the status in a dict. and convert it to dataframe. Then simply send it to a csv file. str(code.getcode()) will return 200 if the url is connecting else it will return an exception, for which i assigned status as '000'. So your csv file will contain url,200 if URL is connecting and url,000 if URL is not connecting.
status_dict={}
for line in lines:
try:
code = urllib.request.urlopen(line)
status = str(code.getcode())
status_dict[line] = status
except:
status = "000"
status_dict[line] = status
df = pd.Dataframe(status_dict)
df.to_csv('filename.csv')

save error compile message in txt file in python3

Could you help me that when I compile my program and if I have an error how can I redirect this error message in text file, before compilation terminate?
I wrote this code, But my problem is i want when i have an error after that my COMP_ERR.txt file create and write error inside this file.
but in my code this create before
import urllib.request
import os
import tempfile
import sys
import fileinput
original_stderr = sys.stderr
f_error = open("COMP_ERR.txt", "w")
sys.stderr = f_error
try:
url = sys.argv[1]
path_snip_file = sys.argv[2]
#url = 'http://pages.di.unipi.it/corradini/Didattica/AP-17/PROG-ASS/03/ClassWithTest.java'
#path_snip_file = "C:/Users/user/AppData/Local/Programs/Python/Python36/snip1.java"
path_remote_file_inOurComp = "C:/Users/user/AppData/Local/Programs/Python/Python36/ClassWithTest.java"
path_remote_file_inOurCom = "C:/Users/user/AppData/Local/Programs/Python/Python36/ClassWithTest1.java"
gt_url = urllib.request.urlretrieve(url)
print("the URL is: ")
print(gt_url)
link = input("input the path of file: ")
"""
f = open(link, "r")
for line in f:
print(line, end='')
f.close()
"""
#--------------------]
#copy snipper java file inside remote file
#[--------------------
with open(path_remote_file_inOurComp, 'w') as modify_file:
with open (link, 'r') as r_file:
for line in r_file:
if " public static void main(" not in line:
modify_file.write(line)
else:
with open(path_snip_file, 'r') as snip_file:
for lines in snip_file:
modify_file.write(lines)
modify_file.write('\n'+line)
#-------------------------------------------------------------
#refactor
#-------------------------------------------------------------
with open(path_remote_file_inOurCom, 'w') as ft:
with open(path_remote_file_inOurComp, 'r') as file_n:
for line in file_n:
line = line.replace("System.out.println(", "System.out.println(insertLength(")
line = line.replace(";", ");")
ft.write(line)
except IndexError:
print("Not enough input! ! !")
sys.stderr = original_stderr
f_error.close()
Check if it is useful to you
import sys
try:
raise
except Exception as err:
**exc_type, exc_obj, exc_tb = sys.exc_info()** # this is to get error line number and description.
file_name = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] # to get File Name.
error_string="ERROR : Error Msg:{},File Name : {}, Line no : {}\n".format(err,file_name,exc_tb.tb_lineno))
file_log = open("error_log.log", "a")
file_log.write(error_string)
file_log.close()
logf = open("download.log", "w")
for download in download_list:
try:
# code to process download here
except Exception as e: # most generic exception you can catch
logf.write("Failed to download {0}: {1}\n".format(str(download), str(e)))
# optional: delete local version of failed download
finally:
# optional clean up code
pass
apart from this approach, you can use logging library to save each and every logs of your application.
Following is the method 2 of the problem of saving logs.
import logging
logging.basicConfig(filename='gunicon-server.log',level=logging.DEBUG,
format='[%(levelname)s]: [%(asctime)s] [%(message)s]', datefmt='%m/%d/%Y %I:%M:%S %p')
try:
# code to process download here
except Exception as e: # most generic exception you can catch
logging.error(str(e))
finally:
# optional clean up code
pass
import sys
sys.stderr = open('errorlog.txt', 'w')
# do whatever
sys.stderr.close()
sys.stderr = sys.__stderr__
see this for more details : https://ubuntuforums.org/showthread.php?t=849752

Why won't this python script generate the desired text file? (despite the script running with no errors)

This script is supposed to generate a text file of stock price values. I can't seem to either find the text file that is supposed to be generated from this script or have this script actually create the desired text file... I added a section of code to check if the file exists, but I keep getting the result that the text file is indeed not created. Please let me know what I can do to correct. When I run the code I do not get any errors. Thanks.
import urllib2
import time
import os
import sys
stockToPull = 'AAPL'
def pullData(stock):
try:
fileLine = stock+'.txt'
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=1y/csv'
sourceCode = urllib2.urlopen(urlToVisit).read()
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine.split(', ')
if len(splitLine)==6:
if 'values' not in eachLine:
saveFile = open(fileLine,'a')
lineToWrite = eachLine+'\n'
saveFile.write(lineToWrite)
print 'Pulled', stock
print 'sleeping'
if os.path.isfile(fileLine): # checks to see if text file created
print "file does exist"
else:
print "No such file"
time.sleep(5)
except Exception, e:
print 'main loop', str(e)
pullData(stockToPull)
You are splitting each row on the string ', ' (note the space). You should be splitting on the comma only:
for eachLine in splitSource:
splitLine = eachLine.split(',')
if len(splitLine)==6:
# etc
You would be better off opening the file once, writing each line to it, then closing the file when finished. You can use a with statement to do this:
with open(fileLine, 'w') as outfile:
for eachLine in splitSource:
splitLine = eachLine.split(',')
if len(splitLine) == 6 and 'values' not in eachLine:
outfile.write('%s\n' % eachLine)
outfile.close()
i think you need to check whether data is arriving or not using
from urllib2 import Request, urlopen, URLError, HTTPError
fileLine = stock+'.txt'
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=1y/csv'
try:
response = urlopen(urlToVisit)
sourceCode = response.read()
<place your logic here>
except HTTPError as e:
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
except URLError as e:
print 'We failed to reach a server.'
print 'Reason: ', e.reason
your code works fine
output i got
['uri:/instrument/1.0/AAPL/chartdata;type=quote;range=1y/csv', 'ticker:aapl', 'Company-Name:Apple Inc.', 'Exchange-Name:NMS', 'unit:DAY', 'timestamp:', 'first-trade:19801212', 'last-trade:20140702', 'currency:USD', 'previous_close_price:59.7843', 'Date:20130703,20140702', 'labels:20130703,20130801,20130903,20131001,20131101,20131202,20140102,20140203,20140303,20140401,20140501,20140602,20140701', 'values:Date,close,high,low,open,volume', 'close:59.2929,94.2500', 'high:60.1429,95.0500', 'low:58.6257,93.5700', 'open:59.0857,94.7300', 'volume:28420900,266380800', '20140620,90.9100,92.5500,90.9000,91.8500,100813200', .....
'20140702,93.4800,94.0600,93.0900,93.8700,28420900', '']
['uri:/instrument/1.0/AAPL/chartdata;type=quote;range=1y/csv']
['ticker:aapl']
['Company-Name:Apple Inc.']
['Exchange-Name:NMS']
['unit:DAY']
['timestamp:']
['first-trade:19801212']
['last-trade:20140702']
['currency:USD']
['previous_close_price:59.7843']
['Date:20130703,20140702']
['labels:20130703,20130801,20130903,20131001,20131101,20131202,20140102,20140203,20140303,20140401,20140501,20140602,20140701']
['values:Date,close,high,low,open,volume']
['close:59.2929,94.2500']
['high:60.1429,95.0500']
['low:58.6257,93.5700']
['open:59.0857,94.7300']
['volume:28420900,266380800']
['20130703,60.1143,60.4257,59.6357,60.1229,60232200']
['20130705,59.6314,60.4700,59.3357,60.0557,68506200']
['20130708,59.2929,60.1429,58.6643,60.0157,74534600']
......
['20140630,92.9300,93.7300,92.0900,92.1000,49482300']
['20140701,93.5200,94.0700,93.1300,93.5200,38170200']
['20140702,93.4800,94.0600,93.0900,93.8700,28420900']
['']
Pulled AAPL
sleeping
file does exist
Your code is fine, but I don't think it is able to do what you want or you don't know what you want. You haven't observed your data correctly. I have made minor changes to your script. Please run this script now:
import urllib2
import time
import os
import sys
stockToPull = 'AAPL'
def pullData(stock):
try:
fileLine = stock+'.txt'
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=1y/csv'
sourceCode = urllib2.urlopen(urlToVisit).read()
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine.split(', ')
if len(splitLine)==6:
print 'Entering outer'
if 'values' not in eachLine:
print 'Entering innter'
saveFile = open(fileLine,'a')
lineToWrite = eachLine+'\n'
saveFile.write(lineToWrite)
print 'Pulled', stock
print 'sleeping'
if os.path.isfile(fileLine): # checks to see if text file created
print "file does exist"
else:
print "No such file"
time.sleep(5)
except Exception, e:
print 'main loop', str(e)
pullData(stockToPull)
If you'd notice, I have just put two print statements inside the if blocks that actually write to a file. On running the script, I noticed that the print statements are never executed. So there are no errors in your code to my knowledge but it doesn't seem to be doing what you want it to. So re-check your data again.
Lastly, to solve these kind of problems, you must use the pdb library which stands for Python Debugger and it is an amazingly helpful tool to debug your code without making a mess of it. Checkout this video from PyCon.

prevent the closure of command Prompt with Python when an "exception" occurs

I have a script in Python 2.7 converted in executable with py2exe. The INPUT data is a text file where the delimiter need to be valid following this function:
# Check if delimeter is valid
def get_parse(filename, delimiters=['\t', ',', ' ', ':', ';', '-']):
with open(filename) as f:
f.next()
secondline = f.next()
for delimiter in delimiters:
if len(secondline.rstrip().split(delimiter)) >= 3:
return delimiter
raise Exception("couldn't find a delimiter that worked!")
When the delimiter is not valid (ex: a dot) i am looking for two solution in a Python elegant way:
Until the right INPUT data is not load you can not pass to OUTFILE
or
The script break the code, show the error, but the windows (when is a
*.exe) doesn't close immediately leaving the user without an explanation
INPUT = raw_input("Input (*.txt): ")
while not os.path.exists(INPUT):
print IOError("No such file or directory: %s" % INPUT)
INPUT = raw_input("Input (*.txt): ")
try:
parse = get_parse(INPUT)
except Exception:
print ValueError("Delimiter type not valid")
break
OUTPUT = raw_input("Output (*.txt): ")
with this solution (break) the Window of my *.exe file close leaving the user without an explanation
You are not really searching for a delimiter, just a character in a string. You should really use the CSV module for this.
from __future__ import print_function
delimiters=['\t', ',', ' ', ':', ';', '-']
def getfile():
fname =""
while fname is "":
fname = str.lower(raw_input("Input(*.txt): "))
while fname.endswith(".txt") is not True:
print ("File must be .txt")
fname = str.lower(raw_input("Input(*.txt): "))
if fname.endswith(".txt"):
try:
with open(fname,'rb') as f:
parsed = False
while not parsed:
data = f.readline()
for d in delimiters:
if d in data:
print ("Delimiter: {0} found".format(d))
parsed = True
# do your additional stuff here
else:
print ("Unable to find delimiter {0}".format(d))
parsed = True
except IOError as e:
print( "Error: ", e)
getfile()
You can hook the exception handler for uncaught exceptions using sys.excepthook, and have it call raw_input() (or input() in 3.x) as per this answer.
For a quick example:
import sys
def wait_on_uncaught_exception(type, value, traceback):
print 'My Error Information'
print 'Type:', type
print 'Value:', value
print 'Traceback:', traceback
raw_input("Press any key to exit...")
sys.excepthook=wait_on_uncaught_exception
Just modify that to have whatever output or whatever you want (I suggest looking into the traceback module).
But if you want it more specific to your code, then just put raw_input("Press any key to exit...") in the solution you already have, and it should be fine. The above should provide a more general solution.

Categories