File filtering_Python 3.2 - windows-7

I'm trying to write a short file filtering code in Python that will find my desired string.
I've got it worked out logically, but my Command Feed is sending me an error message for the print statement. This is how it works as of now:
filename = input('give file name: ')
n = input('give desired string: ')
f = open
line = f.readline()
while line:
if n in line:
print line
line = f.readline()
Error Statement:
Traceback (most recent call last):
File "<string>", line 7, in <fragment>
Syntax Error: print line: <string>, line 718
I know this is a simple problem but the answer is not obvious to me. please help.

print is a function in Python 3. Use print(line) instead.

Related

running my program N times and returning mean running time -bash

I am trying to make a bash file that will rum my program N times and return the average time for the run with certain parameters.
when I try with this code:
#!/bin/sh
NValues=(400)
kValues=(10)
echo "Enter Size(N)"
read N
CURR=0
i=1
sum=0
touch mean_times_log.txt
while [ $i -le $N ]
do
start=`date +%s`
#running my program with arguments
python3.8.5 -m invoke run -k=$1 -n=$2 --no-Random
end=`date +%s`
CURR=$((end - start))
sum=$((CURR + sum))
i=$((i + 1))
done
echo input k = $1 and n = $2 and Random = --Random.Execution mean time over N runs was $((sum/N)) seconds.>> mean_times_log.txt
mv mean_times_log.txt mean_times_log_$( date '+%Y-%m-%d_%H-%M-%S' ).txt
echo file was created
I get errors that I am not sure are connected. my program runs just fine by itself.
the errors:
Traceback (most recent call last):
File "/usr/local/lib/python3.8.5/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/local/lib/python3.8.5/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/__main__.py", line 3, in <module>
program.run()
File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/program.py", line 375, in run
self.parse_tasks()
File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/program.py", line 739, in parse_tasks
result = self.parser.parse_argv(self.core.unparsed)
File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/parser.py", line 169, in parse_argv
machine.handle(token)
File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/parser.py", line 260, in handle
self.see_value(token)
File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/parser.py", line 409, in see_value
self.flag.value = value
File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/argument.py", line 120, in value
self.set_value(arg, cast=True)
File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/argument.py", line 151, in set_value
self._value = func(value)
ValueError: invalid literal for int() with base 10: ''
the k = and n = and Random = --no-Random run ended with 1 seconds
Traceback (most recent call last):
File "/usr/local/lib/python3.8.5/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/local/lib/python3.8.5/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/__main__.py", line 3, in <module>
program.run()
File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/program.py", line 375, in run
self.parse_tasks()
File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/program.py", line 739, in parse_tasks
result = self.parser.parse_argv(self.core.unparsed)
File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/parser.py", line 169, in parse_argv
machine.handle(token)
File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/parser.py", line 260, in handle
self.see_value(token)
File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/parser.py", line 409, in see_value
self.flag.value = value
File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/argument.py", line 120, in value
self.set_value(arg, cast=True)
File "/usr/local/lib/python3.8.5/lib/python3.8/site-packages/invoke/parser/argument.py", line 151, in set_value
self._value = func(value)
ValueError: invalid literal for int() with base 10: ''
would like to know what am I doing wrong or how can I get the results I need. thanks
There are multiple issues:
In bash // is not a comment. Use # instead.
You pass the positional parameters $1 and $2 to your python program. However, it seems like you ran your script without any parameters, so these are empty and your python program complains about the empty string: invalid literal for int() with base 10: ''.
It seems like you wanted to use $kValues and $NValues here (they are currently unused).
kValues=(400) is an array with a single value. Are you sure you want an array?
In bash 5 or higher I would write it this way:
#! /usr/bin/env bash
k=10 n=400
read -p "Enter number of runs: " runs
t=$EPOCHSECONDS
for ((i=0; i<runs; ++i)); do
python3 -m invoke run -k=$k -n=$n --no-Random
done
((t = EPOCHSECONDS - t))
echo "input k = $1 and n = $2 and Random = --Random.Execution" \
"mean time over $runs runs was $((t/runs)) seconds." \
> "mean_times_log_$(date +%Y-%m-%d_%H-%M-%S).txt"
echo "file was created"

QIIME2 dada2 rlang.so error

I was running the QIIME2 moving picture tutorial, at the dada2 step,
I was running:
qiime dada2 denoise-single \
--i-demultiplexed-seqs demux.qza \
--p-trim-left 0 \
--p-trunc-len 120 \
--o-representative-sequences rep-seqs-dada2.qza \
--o-table table-dada2.qza
and ran into this error:
Plugin error from dada2:
An error was encountered while running DADA2 in R (return code 1),
please inspect stdout and stderr to learn more.
Debug info has been saved to /tmp/qiime2-q2cli-err-52fzrvlu.log.
I then open the file: /tmp/qiime2-q2cli-err-52fzrvlu.log. this is what I found:
Running external command line application(s). This may print messages
to stdout and/or stderr. The command(s) being run are below. These
commands cannot be manually re-run as they will depend on temporary
files that no longer exist.
Command: run_dada_single.R
/tmp/qiime2-archive-pco6y5vm/fe614b44-775f-41b1-9ee3-04319005e830/data
/tmp/tmpda8dnyve/output.tsv.biom /tmp/tmpda8dnyve 120 0 2.0 2
consensus 1.0 1 1000000
R version 3.3.1 (2016-06-21) Loading required package: Rcpp Error in
dyn.load(file, DLLpath = DLLpath, ...) : unable to load shared
object '/home/cao/lib/R/library/rlang/libs/rlang.so':
/home/cao/lib/R/library/rlang/libs/rlang.so: undefined symbol:
R_ExternalPtrAddrFn In addition: Warning message: package ‘Rcpp’ was
built under R version 3.4.1 Error: package or namespace load failed
for ‘dada2’ Execution halted Traceback (most recent call last): File
"/home/cao/miniconda3/envs/qiime2-2017.7/lib/python3.5/site-packages/q2_dada2/_denoise.py",
line 126, in denoise_single
run_commands([cmd]) File "/home/cao/miniconda3/envs/qiime2-2017.7/lib/python3.5/site-packages/q2_dada2/_denoise.py",
line 35, in run_commands
subprocess.run(cmd, check=True) File "/home/cao/miniconda3/envs/qiime2-2017.7/lib/python3.5/subprocess.py",
line 398, in run
output=stdout, stderr=stderr) subprocess.CalledProcessError: Command '['run_dada_single.R',
'/tmp/qiime2-archive-pco6y5vm/fe614b44-775f-41b1-9ee3-04319005e830/data',
'/tmp/tmpda8dnyve/output.tsv.biom', '/tmp/tmpda8dnyve', '120', '0',
'2.0', '2', 'consensus', '1.0', '1', '1000000']' returned non-zero
exit status 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"/home/cao/miniconda3/envs/qiime2-2017.7/lib/python3.5/site-packages/q2cli/commands.py",
line 222, in __call__
results = action(**arguments) File "<decorator-gen-252>", line 2, in denoise_single File
"/home/cao/miniconda3/envs/qiime2-2017.7/lib/python3.5/site-packages/qiime2/sdk/action.py",
line 201, in callable_wrapper
output_types, provenance) File "/home/cao/miniconda3/envs/qiime2-2017.7/lib/python3.5/site-packages/qiime2/sdk/action.py",
line 334, in _callable_executor_
output_views = callable(**view_args) File "/home/cao/miniconda3/envs/qiime2-2017.7/lib/python3.5/site-packages/q2_dada2/_denoise.py",
line 137, in denoise_single
" and stderr to learn more." % e.returncode) Exception: An error was encountered while running DADA2 in R (return code 1), please
inspect stdout and stderr to learn more.
I then 'sudo R' and installed the Rcpp and rlang packages, but still got the the same error when I run the same code as I first did:
qiime dada2 denoise-single \
--i-demultiplexed-seqs demux.qza \
--p-trim-left 0 \
--p-trunc-len 120 \
--o-representative-sequences rep-seqs-dada2.qza \
--o-table table-dada2.qza
I figured it out:
it was the R version: i uninstalled R3.4 and install R 3.3 and everythign works

Python: will not read a certain file in a for loop

I have a directory containing files and they are all processed except one, file2.txt with my the_script.py script.
Independanty i ran a simple for line in file2.txt: print line and it worked just fine. The lines were printed. So the file is not the problem, it is formatted just as the other ones (automatically, output of another script).
Here is the_script.py :
#!/usr/bin/python
import os
import glob
#[...]rest of the code not dealing with the files in questions
for filename in glob.glob("outdir/*_mapp"): #i need to get all the files in outdir/ directory with the *_mapp extension
infilemapp=open(filename)
print "start"
print infilemapp #test, priting all filenames
organism=(filename.split("/", 1)[1])[:-5] # outdir/acorus.txt_mapp --> acorus.txt IRRELEVANT PARSING LINE
infilelpwe=organism+"_lpwe" #acorus.txt --> acorus.txt_lpwe IRRELEVANT PARSING LINE
for line in infilemapp:
print line
print "end"
What i expected is to get, for ALL files, "start, filename, filecontent, end". I get in console:
bash-4.3$ ./the_script.py
start
<open file 'outdir/file1.txt_mapp', mode 'r' at 0x7fb5795ec930>
['3R', '2F', '0R', '3F', '1R', '4F', '1F']
end
start
<open file 'outdir/file3.txt_mapp', mode 'r' at 0x7fb5795eca50>
['0R', '5R', '7R', '4R', '1F', '6R', '2R', '6F', '1R', '4F', '7F', '5F', '0F', '3R']
end
start
<open file 'outdir/file2.txt_mapp', mode 'r' at 0x7fb5795ec930>
end
As you can see, nothing is printed for file2.txt_mapp.
bash-4.3$ cat outdir/file2.txt_mapp
['5F', '0F', '2F', '6F', '3R', '5R', '6R', '4F', '1R', '4R', '6F']
The file is alphabetically in the middle of all files. Why does my script not work for this specific one? Please if you have any suggestions...

Calling Raspberry Pi temp sensor, invalid syntax when calling driver as bash script

I'm currently putting together a temperature sensor for my Raspberry Pi and ran into this issue. In order to run the driver designated in the code, I had to run it was a shell command. I'm having a difficult time figuring out why my syntax is incorrect.
Code:
def read_dht22( PiPin ):
output = subprocess.check_output([shell=True], ['/home/pi/Adafruit-Raspberry-Pi-Python-Code/Adafruit__DHT_Driver", "2302", str(PiPin)])
Error:
sudo python scr6.py
File "scr6.py", line 31
output = subprocess.check_output([shell=True], ['/home/pi/Adafruit-Raspberry-Pi-Python-Code/Adafruit_DHT_Driver', '2302', str(PiPin)])
^
SyntaxError: invalid syntax
Error Prior to Adding shell=True
sudo python scr5.py
Traceback (most recent call last):
File "scr5.py", line 46, in <module>
temp_c, temp_f = read_dht22(4)
File "scr5.py", line 31, in read_dht22
output = subprocess.check_output(["/home/pi/Adafruit-Raspberry-Pi-Python-Code/Adafruit_DHT_Driver", "2302", str(PiPin)])
File "/usr/lib/python2.7/subprocess.py", line 537, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied
This should fix your syntax error:
def read_dht22( PiPin ):
output = subprocess.check_output(["/home/pi/Adafruit-Raspberry-Pi-Python-Code/Adafruit__DHT_Driver", "2302", str(PiPin)], shell=True)

Python: How to pass non-ASCII file names to Popen on Windows?

When I run a subprocess with Python, everything id fine with ASCII params but it fails if a parameter is an unicode (cyrillic) string:
cmd = [ 'dir.exe', u'по-русски' ]
p = subprocess.Popen([ 'dir.exe', u'по-русски' ])
Error log:
Traceback (most recent call last):
File "process.py", line 48, in <module>
cyrillic()
File "process.py", line 45, in cyrillic
p = subprocess.Popen(cmd, shell=True, stdin=None, stdout=None, stderr=subprocess.PIPE)
File "C:\Python\27\Lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Python\27\Lib\subprocess.py", line 870, in _execute_child
args = '{} /c "{}"'.format (comspec, args)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 8-10: ordinal not in range(128)
I tried different executables - 7z.ex, ls.exe - popen fails before even running them.
But what if I encode unicode string to a specific encoding?
# it works because 1251 is kinda native encoding for my Windows
cmd = [ 'dir.exe', CYRILLIC_FILE_NAME.encode('windows-1251') ]
# fails because 1257 cannot be converted to 1251 without errors
cmd = [ 'dir.exe', BALTIC_FILE_NAME.encode('windows-1251') ]
# this may work but it's not a solution because...
cmd = [ 'dir.exe', BALTIC_FILE_NAME.encode('windows-1257') ]
The "bad" thing, I have different file names on my computer - baltic, cyrillic and many more. So it looks there is no general way to pass a non-ASCII file name to Popen on Windows?! Or could that be fixed still? (Without dirty hacks preferably.)
Windows 7, Python 2.7.3
If you use Python 3, it will pass the parameters properly as Unicode. Assuming your subprocess can load unicode parameters on the command line (Python 2 cannot), then it should work.
For example, this script, when run under Python 3, will display the Cyrillic characters.
import subprocess
subprocess.call(["powershell", "-c", "echo", "'по-русски'"])

Resources