I am creating a program to read a FASTA file and split at some specifc characters such as '>' etc. But I'm facing a problem.
The program part is:
>>> def read_FASTA_strings(seq_fasta):
... with open(seq_fasta.txt) as file:
... return file.read().split('>')
The error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'seq_fasta' is not defined
How to get rid of this problem?
You need to specify the file name as a string literal:
open('seq_fasta.txt')
You need to quote the filename: open('seq_fasta.txt').
Besides that, you might choose a different name but file as using this name shadows a builtin name.
Your program is seeing seq_fasta.txt as a object label, similar to how you would use math.pi after importing the math module.
This is not going to work because seq_fasta.txt doesnt actually point to anything, thus your error. What you need to do is either put quotes around it 'seq_fasta.txt' or create a text string object containing that and use that variable name in the open function. Because of the .txt it thinks seq_fasta(in the function header) and seq_fasta.txt(in the function body) are two different labels.
Next, you shouldnt use file as it is an important keyword for python and you could end up with some tricky bugs and a bad habit.
def read_FASTA_strings(somefile):
with open(somefile) as textf:
return textf.read().split('>')
and then to use it
lines = read_FASTA_strings("seq_fasta.txt")
Related
path of file is:
"C:\Users\deana\OneDrive\Marlon's files\Programming\Python\PITT\PITT_LIbrary\Lists\test.txt"
lines of code are:
import os
os.chdir("C:/Users/deana/OneDrive/Marlon's files/Programming/Python/PITT/PITT_LIbrary/Lists")
exec(open('test.txt'))
the error is this:
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
exec(open('test.txt'))
TypeError: exec() arg 1 must be a string, bytes or code object
also if I try on one line as such:
exec(open(r"C:/Users/deana/OneDrive/Marlon's files/Programming/Python/PITT/PITT_LIbrary/Lists/test.txt"))
i'ts the same error. (with and without r)
super frustrationg as it reads like i'm not inputting string... but it is string!?!
also I've done this litteraly the same way before, restarted IDLE shell, no difference.
ugh! I always get stupid errors with file paths.
I should have been using os.startfile() to open this.
It was confusing by using .open(). as I was attempting to open in default app.
before, i've used exec.open() to open .py files and guess I got them confused.
exec is just used to open other scripts... need stronger coffee next time.
Try this:
import os
os.chdir("C:/Users/deana/OneDrive/Marlon's files/Programming/Python/PITT/PITT_LIbrary/Lists")
exec(open('test.txt', 'rb'))
You can convert the txt file to bytes by opening it with rb (read bytes).
I know this is an easy fix, but could someone tell me how to call a python file in the python Console who have this symbol: -.
Here is my mistake:
>>> import main #no error here
>>> import a1-devoir1
File "<input>", line 1
import a1-devoir1
Syntax Error: invalid syntax
You must name your files so that they only contains letters, underscores, or numbers (but not as the first character). All libraries and modules must follow this.
So rename your .py file to a1_devoir and then try import a1_devoir
I am new to Python and am wondering how to address the following attribute error. I believe I need to define/declare the file variable? Thanks for any suggestions, here is my script:
AttributeError Traceback (most recent call last)
in
51
52 # Write methods to print to Financial_Analysis_Summary
---> 53 file.write("Financial Analysis")
54 file.write("\n")
55 file.write("----------------------------")
AttributeError: 'str' object has no attribute 'write'
From your code and error, I think You've defined the variable 'file' as a string. Aslo there is no attribute write() in the class str. Hence, the reason for this error. For more information, include the whole script i.e., mainly the use of variable 'file'. I think you can use print() to print the above mentioned details or create a new class with a method inside to print your desired things
It looks like you've somehow defined file as a string rather than a file. What you should to is define it thus:
summary_file=open("C:/someFolder/someOtherFolder/Financial_Analysis_Summary.txt",
mode='r+', encoding='utf8')
and then write to it.
The first argument to the open function is the file path. The mode is how you want to access the file: 'r' lets you read the file and nothing else (and throws a FileNotFoundError if the file doesn't yet exist; the others just create it), 'r+' lets you write to the file while leaving its preexisting text in place (although if you write to the middle of the file you'll still overwrite whatever was there), 'w' deletes what was in the file and lets you write to it, 'a' lets you write text only to the end of the file, 'w+' and 'a+' are the same as w and a except they let you read from the file; you can add b to the end of any of these to interact with the file in the form of bytes rather than strings. The encoding should only matter if you plan to use Unicode characters, in which case set it to the same encoding you'll use to view the file (usually 'utf8') to avoid garbling non-ASCII characters.
I am creating a program that stores options in a txt file, and I tried a few different lines but so far I think an eval is the best one.
I got this info in the file:
colordefondo = "#abcdef"
and I got this eval in the program:
for line in open("filename.txt", "r"):
eval(line)
However, when I try the code it gives me this error:
Traceback (most recent call last):
File "D:\project-sae\Sae.py", line 25, in <module>
eval(line)
File "<string>", line 1
colordefondo = "#abcdef"
^
SyntaxError: invalid syntax
My question is Why? And if anyone knows a better way to load and store the value of several options from a txt file it would be great. Nevertheless my question is about why that eval is failing, my knowledge about how eval works seems wrong and I don't know why, used it several times before, just never from a file.
As the documentation says, 'eval' evaluates an expression. Not a statement. Assignments are statements.
You could use the exec statement. But if you want to exec all of the lines of a file, that's exactly what execfile does. (I'm assuming Python 2.x here; things are a little different in 3.x, but the idea is similar.)
However, this is a bad idea to do in the first place. A better way to load and store the value of several options from a txt file is to use anything other than Python source as your format. JSON, .ini files, .rc files, etc. Python comes with code built-in to handle a variety of such formats. See File Formats and Internet Data Handling for a list of the relevant modules, read through them, and pick the one you want.
I am trying to open python prompt and run the below code:
>>> a=open("Andrew_Smith_(author/education_professional)_0",'w')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory:
'Andrew_Smith_(author/education_professional)_0'
I am not sure why I am getting the error. I know the file contains special characters, but I am asking it to create a new file.
Edit:
I cannot use -, as some names might contain '-'. I also don't want to use space. Is there any other alternative?
As others have said the issue is the /, it is looking for a directory named Andrew_Smith_(author to create the new file education_professional)_0 in.
bash-3.2# mkdir "Andrew_Smith_(author"
bash-3.2# python
>>> a=open("Andrew_Smith_(author/education_professional)_0", 'w')
>>>
Because you have a / character in your filename. Neither *NIX nor Windows typically allow this.
I believe there are two problems here. First is because of the '/' character. It can't distinguish between / in a file name (which isn't valid) and / as a path seperator. And second, I don't believe '(' or ')' are valid in path names either.
The problem is that you are using reserved characters -- remove the / (or replace it with, for example, -) and everything should just work.