Syntax error near unexpected token '(' after implementing python program - python

I initally ran RepeatMasker on a Mac, and I am attempting to analyze the file on a Linux OS.
When I try to run my program Unique_Repeat_Code.py:
RM_output=open('genomic.fna.out')
contents=RM_output.read'()'
unique_repeats_DNA=open('genomic_unique_repeats.txt','w')
for _ in contents:
bit_score=column[0]
percent_div=column[1]
percent_del=column[2]
percent_ins=column[3]
query_seq=column[4]
position_query_begin=column[5]
position_query_end=column[6]
left=column[7]
sense=column[8]
matching_repeat=column[9]
repeat_class=column[10]
position_in_repeat_begin=column[11]
position_in_repeat_end=column[12]
left2=column[13]
ID=column[14]
while matching_repeat:
unique_repeats=set(matching_repeat)
unique_repeats_DNA.write(unique_repeats)
unique_repeats_DNA.close()
in terminal I get the following error:
./Unique_Code.py: line 6: syntax error near unexpected token '('
./Unique_Code.py: line 6: 'RM_output=open('genomic.fna.out')'
Any help in fixing this problem would be appreciated. Thank you in advance!

You are running your script as a bash script when you use the "./" syntax. You need to use the python interpreter to run your script:
python Unique_Code.py

Related

Unable to find output file in ubuntu generated by python

I am currently working on python 3.6 using spider. I have written a code which in theory works in windows but it does not on ubuntu 18.04. My problem is that I want to write my results on a text file but it is nowhere to be found.
I write the following:
with open('Vx1.txt', 'w+') as fv1:
for itemv1 in var:
fv1.write("%s\n" % itemv1)
fv1.close()
But I am unable to find the output file Vx1.txt if there is one at all. I have tried to search from files as if I were working on a windows device and when it has not worked out I have used the console comand:
/$ find ~/ -type f -name "Vx1"
The computer does not return anything at all, just goes back to the prompt.
Can someone please tell me how can I find my output?
Please note that I have tried with single and double commas over the words when searching.
Thanks in advance.
Could it be indentation issues?
with open('Vx1.txt', 'w+') as fv1:
for itemv1 in var:
fv1.write("%s\n" % itemv1)

Wildcard error - "invalid option"

I am currently working on a python script in which there is a moment I want to delete a file which name is ending with .txt
To do so I just run a command line using os in python:
os.system("del working/*.txt")
When running the python script, I get the following error in cmd:
Option non valide - "*". which can be translated "Invalid option"
It seems that the wildcard isn't recognized by cmd but I know very little about this. Why is it not working ?
I know I could handle the situation with regular expressions but I'd like to understand.
Thank you in advance
In Windows, \ is the path delimiter, not /, so you should do:
os.system(r"del working\*.txt")
Note that / in Windows is for switches, hence the "invalid option" error.
I think its better use os.remove instead os.system with "del" command. Using os.system your script will not work on linux. Here a example using os.remove:
files = os.listdir("working\")
for fi in files:
if fi.endswith(".json"):
os.remove("working\{}".fomat(fi))

Cronjob is returning EOF error while looking for matching ``'

I am running a cronjob script which for some reason stopped working. I may have missed a typo or something in my cronjob file.
I keep getting an EOF error while looking for matching ``'
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file
Here is my cron job.
* * * * * /bin/bash `python /Users/reid/Documents/Programs/sortphotos/src/sortphotos.py --keep-duplicates --sort \%Y/\%m-\%B --rename %Y-%m-%d_%H-%M-%S --day-begins 4 /Users/reid/Dropbox/Camera\ Uploads/ /Users/reid/Dropbox/Camera\ Uploads/`
I know my actual python command runs and executes when I run it. I figured it could be the lack of knowing where python is but this was working before.
What could be the issue with the EOF?
Try this:
* * * * * python /Users/reid/Documents/Programs/sortphotos/src/sortphotos.py --keep-duplicates --sort \%Y/\%m-\%B --rename %Y-%m-%d_%H-%M-%S --day-begins 4 /Users/reid/Dropbox/Camera\ Uploads/ /Users/reid/Dropbox/Camera\ Uploads/
drop /bin/bash
drop the back quotes
I figured out my issue. If I leave my cron job command written as is, the issue is where I am adding the parameter to rename the file. The attributes I am using to configure the names,
--rename %Y-%m-%d_%H-%M-%S
need to be escaped, so:
--rename \%Y-\%m-\%d_\%H-\%M-\%S
Thanks for your help.

What is wrong with this python command?

Let me first say that I'm a newbie to python, and I've written a python shell named test.py. The content is as follows:
#!/usr/bin/python
import os
cur_dir = os.getcwd();
print 'hello world' > cur_dir+"/test.log" 2>&1
When I run test.py by "python test.py", it says:
File "test.py", line 4
print 'hello world' > cur_dir+"/test.log" 2>&1
^
SyntaxError: invalid syntax
Could anyone give me some idea? Thanks!
P.S.
what I want is to write a string in test.log file. That's all. As I said first, I'm REALLY new to python, so please don't be so harsh to me :), I'm gonna read through the tutorial first to have a glimpse of python, Thanks to all you guys!
You are using invalid syntax. As a result Python gives you the error:
SyntaxError: invalid syntax
To fix this, stop using the invalid syntax.
Also in the future, explain what you are trying to achieve, and your answers are likely to tell you how to achieve that.
The invalid syntax looks like it's a bash redirection of stderr to stdout which makes no sense in the context above, so it's not possible to figure out what you are actually trying to do.
To write to a file you do this:
thefile = open('path_to_the_file', 'wt')
thefile.write('The file text.\n')
However, to do logging, you are better off using the logging module.
Use '>>' with print to redirect it to an alternate stream.
See How to print to stderr in Python?

Python invalid syntax in print

Sorry I am not familar with Python...
It gives me the following error message
File "gen_compile_files_list.py", line 36
print 'java files:', n_src
^
SyntaxError: invalid syntax
I.e. caret points to last quote. What's wrong with it?
OS Windows 7, Python version 3.2.2
On Python 3, print is a function. You need this:
print('java files:', n_src)
print changed syntax between Python2 and Python3; it is now a function.
You would need to change:
print 'java files:', n_src
to
print('java files:', n_src)
Alternatively, you can try converting the code from Python2 to Python3 syntax with the 2to3 tool. Here is more information on the transition if you are interested. This way you can maintain a single code base that works on both versions.
As you are not familiar with python, try installing Python 2 instead and running the code with that.
print is a function in Python 3+. So:
print ('java files:', n_src)

Categories

Resources