Why will this function not open my file? [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
def open_file(filename):
file_open= open(filename,"r")
return file_open
When I try and call the function I get the following results:
>>> open_file(random.txt)
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
open_file(random.txt)
NameError: name 'random' is not defined

try
open_file('random.txt')
Strings in Python need to be quoted.
random is being interpreted as an object, and is undefined.

You forgot quotes:
open_file('random.txt')
python thinks random is an object, which obviously you didn't define. The quotes make it a string.

you just need to input the filename as a string; here's how it must be done:
>>> open_file('random.txt')
note that your function works just fine, all you need to do is call it properly.

Related

I keep getting errors with an "if var.startswith" statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I'm trying to make it so that I can input someone into console and have it set a variable to it, and every time with the if statement it gives the error
File "main.py", line 58, in <module>
if meInput.startswith("%send"):
AttributeError: 'builtin_function_or_method' object has no attribute 'startswith'
Here's the code:
if input.startswith("%send"):
myinput = input.split(" ", 2)[2]
channel = client.get_channel(12324234183172)
I've tried putting it into a variable such as variable = input then changing the if statement to match the variable, but it does the same thing.
Read the error message carefully! It is telling you that input is not a string, but a function — a function that would return a string if you called it, but you didn’t. Try this instead:
if input().startswith("%send"):
Note the parentheses. That is how you call a function in Python, and in most other languages.

AttributeError: 'str' object has no attribute '__name__' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
What is causing this error? Isn't it possible commenting out lines like this inside code?
for i in (Class_1, """Class_2, Class_3"""):
name = i.__name__
Class_1, Class_2 and Class_3 are classes declared before the upper code.
Error output:
> Traceback (most recent call last):
File "", line 2, in <module>
name = i.__name__
AttributeError: 'str' object has no attribute '__name__'
Process finished with exit code 1
Error message line edited to fit the example code
Remove the triple-quoted string """Class_2, Class_3""" to avoid iterating over it which is what you're doing in this case so it looks like for i in (Class_1,) (parenthesis are optional).
It seems you want to comment out those unnecessary sides, but please note that those triple-quotes strings technically aren't comments, so they can still affect the script in some areas you didn't intend.
What do you mean by
for i in (Class_1, """Class_2, Class_3"""):
When you iterate over this tuple, the second element is a string, thus causing the error.

error in writing a text file in python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am writing a text file with the following code(pl in the code is a list of lists):
out_file = open("par.txt", 'w')
out_file.write("id\ttrans_id\ttype\tstatus\tname\ttrans_type\ttrans_status\ttrans_name\n")
for lst in pl:
out_file(lst[0].split()[1],"\t",lst[1].split()[1],"\t",lst[2].split()[1],"\t",lst[3].split()[1],"\t",lst[4].split()[1],"\t",lst[5].split()[1],"\t",lst[6].split()[1],"\t",lst[7].split()[1])
out_file.close()
BUT it gives this error:
Traceback (most recent call last):
File "test.py", line 25, in <module>
out_file(lst[0].split()[1],"\t",lst[1].split()[1],"\t",lst[2].split()[1],"\t",lst[3].split()[1],"\t",lst[4].split()[1],"\t",lst[5].split()[1],"\t",lst[6].split()[1],"\t",lst[7].split()[1])
TypeError: 'file' object is not callable
You need to change the loop to something like:
for lst in pl:
out_file.write('\t'.join(x.split()(1) for x in lst))
out_file.write('\n')
Try this:
with open("par.txt", "a+") as f:
f.write("id\ttrans_id\ttype\tstatus\tname\ttrans_type\ttrans_status\ttrans_name\n")
for lst in pl:
f.write(("{}\t"*8).format(lst[0].split()[1],lst[1].split()[1],lst[2].split()[1],lst[3].split()[1],lst[4].split()[1],lst[5].split()[1],lst[6].split()[1],lst[7].split()[1]))
It's not going to overwrite your file.You should change that 8 variable what is the length of your list, which is we dont know that list.
Example Output:

Python 3.3 NameError [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
>>> a=int(input())
2
>>> b=int(input())
4
>>> c=input()
r
>>> if c==r:
print(b+1)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
if c==r:
NameError: name 'r' is not defined
The function input() returns a string, so if you want to check if c is the string r, you have to add double quotes, or single quotes. Both are used to represent strings in Python:
if c == 'r':
or
if c == "r":
You have to check with "r" not r. As follows:
>>>if c=='r':
>>> print(b+1)
5
This is because when you input "r" it is as a string. When you did the c==r you were comparing to the variable r. This is clearly not what you wanted. Therefore you use c=='r' to compare strings.

How to declare and fill an empty array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I got an error using numpy.zeros, it seems like my value a can't be filled since i got an error:
track=2
a=np.zeros(shape=(3,2))
eps_real=a(Cp-0.5,2)/2*3.14*track
eps_imag=a(Cp-0.5,2*track)/2*3.14*track
tau=a(Cp-1,2)
print tau
My error when i ran is:
Traceback (most recent call last):
File "Main.py", line 35, in <module>
eps_real=a(Cp-0.5,2)/2*3.14*track
TypeError: 'numpy.ndarray' object is not callable
Collection members in Python use square brackets ([]), not parentheses. So your code should be:
eps_real=a[Cp-0.5,2]/2*3.14*track
eps_imag=a[Cp-0.5,2*track]/2*3.14*track
tau=a[Cp-1,2]
Parentheses are used for calling functions, hence the error message object is not callable

Categories