Casper.js unable to open JSON file when activated through python script - python

I have a CasperJS script that opens a JSON file of search terms and parses a page looking for them. It works fine when I run it from the command line.
var casper = require('casper').create();
var fs = require('fs')
var names = fs.read('/var/www/html/tracker/names.json');
...
But when I try to run it through a shell script using Python, it has difficulty reading the JSON file. fs.read returns "".
The Python script:
app = subprocess.Popen("/usr/local/bin/casperjs /var/www/html/tracker/scraper.js", shell=True)
app.wait()
out, errs = app.communicate()

Figured it out. Naturally, my own boneheaded mistake.
Didn't make this clear in the question, but the Python script first prepares names.json before running the scraper through a subprocess. So if we add a little bit to the code snippet shown above:
# Save JSON file
f = open('names.json', 'w')
f.write( json.dumps(json_ready, sort_keys=True, indent=4) )
# Query scraper
app = subprocess.Popen("/usr/local/bin/casperjs /var/www/html/tracker/scraper.js", shell=True)
app.wait()
out, errs = app.communicate()
The problem? I forgot to close f.
# Save JSON file
f = open('names.json', 'w')
f.write( json.dumps(json_ready, sort_keys=True, indent=4) )
f.close()

Related

Python code working on terminal but not on web server

I'm executing these lines of code on python terminal and it is working but when i execute it on webserver which is xampp i get output 255 in transcribe_text.txt file but when i run through terminal i get audio to text proper conversion. I'm working on windows environment.
import subprocess
out=subprocess.getstatusoutput(['deepspeech', '--model', 'C:/deepspeech/deepspeech-0.6.0-models/output_graph.pb', '--lm', 'C:/deepspeech/deepspeech-0.6.0-models/lm.binary', '--trie', 'C:/deepspeech/deepspeech-0.6.0-models/trie', '--audio', 'C:/deepspeech/audio/8455-210777-0068.wav'])
#open and read the file after the appending:
print(str(out))
f = open("transcribe_text.txt", "a")
f.write(str(out))
f.close()
#open and read the file after the appending:
f = open("transcribe_text.txt", "r")
print(f.read())
Any solution Please, Thanks

python readlines not working during incron

I'm trying to call a python script through incron:
/data/alucard-ops/drop IN_CLOSE_WRITE /data/alucard-ops/util/test.py $#/$#
but I cant seem to read from the file passed. Here is the script:
#!/usr/bin/env /usr/bin/python3
import os,sys
logfile = '/data/alucard-ops/log/'
log = open(logfile + 'test.log', 'a')
log.write(sys.argv[1] + "\n")
log.write(str(os.path.exists(sys.argv[1])) + "\n")
datafile = open(sys.argv[1], 'r')
log.write('Open\n')
data = datafile.readlines()
log.write("read\n")
datafile.close()
The output generated by the script:
/data/alucard-ops/drop/nsco-20180219.csv
True
Open
It seems to stop at the readlines() call. I dont see any errors in the syslog.
Update: It seems that i can use a subprocess to cat the file and it retrieves the contents. But, when i decode it, data.decode('utf-8') I'm back to nothing in the variable.
I ended up using watchdog instead.

How to execute a python script and write output to txt file?

I'm executing a .py file, which spits out a give string. This command works fine
execfile ('file.py')
But I want the output (in addition to it being shown in the shell) written into a text file.
I tried this, but it's not working :(
execfile ('file.py') > ('output.txt')
All I get is this:
tugsjs6555
False
I guess "False" is referring to the output file not being successfully written :(
Thanks for your help
what your doing is checking the output of execfile('file.py') against the string 'output.txt'
you can do what you want to do with subprocess
#!/usr/bin/env python
import subprocess
with open("output.txt", "w+") as output:
subprocess.call(["python", "./script.py"], stdout=output);
This'll also work, due to directing standard out to the file output.txt before executing "file.py":
import sys
orig = sys.stdout
with open("output.txt", "wb") as f:
sys.stdout = f
try:
execfile("file.py", {})
finally:
sys.stdout = orig
Alternatively, execute the script in a subprocess:
import subprocess
with open("output.txt", "wb") as f:
subprocess.check_call(["python", "file.py"], stdout=f)
If you want to write to a directory, assuming you wish to hardcode the directory path:
import sys
import os.path
orig = sys.stdout
with open(os.path.join("dir", "output.txt"), "wb") as f:
sys.stdout = f
try:
execfile("file.py", {})
finally:
sys.stdout = orig
If you are running the file on Windows command prompt:
python filename.py >> textfile.txt
The output would be redirected to the textfile.txt in the same folder where the filename.py file is stored.
The above is only if you have the results showing on cmd and you want to see the entire result without it being truncated.
The simplest way to run a script and get the output to a text file is by typing the below in the terminal:
PCname:~/Path/WorkFolderName$ python scriptname.py>output.txt
*Make sure you have created output.txt in the work folder before executing the command.
Use this instead:
text_file = open('output.txt', 'w')
text_file.write('my string i want to put in file')
text_file.close()
Put it into your main file and go ahead and run it. Replace the string in the 2nd line with your string or a variable containing the string you want to output. If you have further questions post below.
file_open = open("test1.txt", "r")
file_output = open("output.txt", "w")
for line in file_open:
print ("%s"%(line), file=file_output)
file_open.close()
file_output.close()
using some hints from Remolten in the above posts and some other links I have written the following:
from os import listdir
from os.path import isfile, join
folderpath = "/Users/nupadhy/Downloads"
filenames = [A for A in listdir(folderpath) if isfile(join(folderpath,A))]
newlistfiles = ("\n".join(filenames))
OuttxtFile = open('listallfiles.txt', 'w')
OuttxtFile.write(newlistfiles)
OuttxtFile.close()
The code above is to list all files in my download folder. It saves the output to the output to listallfiles.txt. If the file is not there it will create and replace it with a new every time to run this code. Only thing you need to be mindful of is that it will create the output file in the folder where your py script is saved. See how you go, hope it helps.
You could also do this by going to the path of the folder you have the python script saved at with cmd, then do the name.py > filename.txt
It worked for me on windows 10

Python 2.7 to exe using py2exe issue

I successfully created an .exe using py2exe with a simple test script I found on a tutorials website. The script I am using, however, does not seem to work. My code uses the csv module and dict reader with two .csv inputs.
I run the python setup.py p2exe command, and I get a flash of a command prompt, but that disappears before I can read anything on it. And once it disappears, I do not have the correct .csv file output that I would get if I just ran the script in python.
Can anyone offer any advice or things to try? Or is there a way I could get that pesky cmd window to stay open long enough for me to see what it says?
Thanks. My script is below.
import csv
def main():
iFileName = 'DonorsPlayTesting.csv'
oFileName = iFileName[:-4] + '-Output' + iFileName[-4:]
iFile = csv.DictReader(open(iFileName))
oFile = csv.writer(open(oFileName, 'w'), lineterminator = '\n')
iDirectory = csv.DictReader(open("DonorsDirectory.csv"))
oNames = {}
directory = {}
for line in iDirectory:
directory[line['Number']] = line['Name']
for key in directory.keys():
oNames[directory[key]] = 0
out_header = ['Name', 'Plays']
oFile.writerow(out_header)
for line in iFile:
if line['Type'] == "Test Completion":
if line['Number'] in directory:
oNames[directory[line['Number']]] += 1
elif line['Number'] not in directory:
oNames[line['Number']] = 'Need Name'
oFile.writerows(oNames.items())
main()

writing output of python cgi.print_environ() and cgi.print_form() to file

I'm trying to capture the output of cgi.print_form() and cgi.print_environ() and write them to files for later perusal, but it doesn't seem to be behaving as I'd expect.
I would expect this to simply write the output of the module to the path specified, but what I get is the cgi.print_form() output displayed to the page, nothing from cgi.print_environ() , the formpath file is empty, and the envpath file doesn't get created.
When I wrap the module in str() I get None in both files.
This was supposed to be a quick test to see what I'm getting from a remotely hosted form, but it just isn't working.
It's python 2.7.2
formfile = open(formpath, 'w')
formfile.write(cgi.print_form(form))
formfile.close()
envfile = open(envpath, 'w')
envfile.write(cgi.print_environ())
envfile.close()
EDIT: Just decided to write my own loop, but am still looking for a way to capture the print_form and print_environ function output.
for item in form:
key = form.getfirst(item)
fileitem = form[item]
if fileitem.filename:
formfile.write("%s is file %s\n" % (item, fileitem.filename))
filepath = '../fmtmp/%s_%s_%s' % (tstamp, item, fileitem.filename)
open(filepath, 'w').write(fileitem.file.read())
else:
formfile.write("%s = %s\n" % (item, key))
formfile.close()
envfile = open(envpath, 'w')
for var in os.environ:
envfile.write("%s = %s\n" % (var, os.environ[var]))
envfile.close()
Which gives me the added benny of saving whatever is uploaded as a file.
Note that x = cgi.print_environ() still outputs stuff if run in the interactive prompt (x will be None). CGI is interacting witht the web server over standard in/out. print_environ() is simply printing to standard out. It's point is to make it easy to debug enviroment by printing it in your browser.
EDIT: Actually sending it to file is somewhat tricky. You could try:
f = open('file', 'w')
old = sys.stdout
sys.stdout = f
cgi.print_environ()
f.flush()
sys.stdout = old
f.close()

Categories

Resources