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
Related
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.
Code and Error messageI am unable to open text file in python despite of writing correct code. Screenshot of code, error and file location is as followsText file location
You should enter adnan.txt as input, since it's a text file.
You have to change the lines to:
fhand = open(raw_input("Enter a filename: "),'r')
And then the rest of the code
edit
As John Smith says, be careful with the name you are trying to use, in your case seems to be adnan.txt
I attach here a piece of code that could work for you:
import os
MY_FILENAME = "adnan.txt" # don't forget the extension
with open(os.path.join(os.getcwd(),'MY_FILENAME'), 'r') as my_file:
f = myfile.readlines()
for line in f:
print(line)
I used os.getcwd() as your file seems to be in the script directory
This question already has an answer here:
Process substitution
(1 answer)
Closed 8 years ago.
I am writing a bash script that uses an argument from the command line input to pass into a python script, the result is using python's csv.writer module to produce a .csv file. I then have written an R script which accepts a .csv file on it's own, but I now want to pipe the csv file directly from my python script into my r script.
Here is my bash script:
#!/bin/bash
python protparams.py $1 | Rscript frequency.r
and my python script:
from Bio import SeqIO
from Bio.SeqUtils import ProtParam
from Bio.SeqUtils import ProtParamData
import sys
import csv
handle = open(sys.argv[1])
with open('test.csv', 'w') as fp:
writer = csv.writer(fp, delimiter=',')
for record in SeqIO.parse(handle, "fasta"):
seq = str(record.seq)
X = ProtParam.ProteinAnalysis(seq)
data = [seq,X.get_amino_acids_percent(),X.aromaticity(),X.gravy(),X.isoelectric_point(),X.secondary_structure_fraction(),X.molecular_weight(),X.instability_index()]
writer.writerow(data)
which all works fine up to here my python script generates the csv file when called via my bash script. great! but when I pipe it into the following R script as in my bash file I get this error:
Error in file(file, "rt") : cannot open the connection
Calls: read.csv -> read.table -> file
In addition: Warning message:
In file(file, "rt") : cannot open file 'NA': No such file or directory
Execution halted
Here is my R script for reference:
args <- commandArgs(trailingOnly = TRUE)
dat <- read.csv(args[1], header=TRUE)
write.csv(dat, file = "out2.csv")
(at the moment my r script is simply testing to see if it can output the .csv file).
This message normally occurs when the file doesn't exist, however in this case I think it is appearing because the argument in my r script is expecting a file passed as a command line argument - which just isn't getting picked up in the current way I have written my bash script. Am I wrong in thinking that piping the output of my python program is the same as using the output as a command line argument for my r script?
Thanks very much.
Yes, a little change in frequency.R (reading from stdin) allows to duplicate a .csv:
l#np350v5c:~$ cat foo.sh
cat gbr_Country_en_csv_v2.csv | Rscript frequency.R
l#np350v5c:~$ cat frequency.R
f <- file("stdin")
dat <- read.csv(f, header=TRUE)
write.csv(dat, file = "out2.csv")
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
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()