-5

i want to try to open up a file, find the index of a particular string (S_N) then seek the position and write something underneath.

i am using the code...

C1 = open("Class 1.txt", "a")
pos = C1.find(S_N)
C1.seek(pos)
C1.write(correct)

and every time i use it, python throws up this error...

Traceback (most recent call last):
  File "C:\Users\eddie\Documents\HOMEWORK!!!!\11c\Computing Science\controlled assessment help\task3\task 3 official.py", line 41, in <module>
    pos = C1.find(S_N)
AttributeError: '_io.TextIOWrapper' object has no attribute 'find'

WHY DOES THIS NOT WORK????

  • Well, because `'_io.TextIOWrapper' object has no attribute 'find'`, what is `S_N`? What is that file content? – tglaria Jan 26 '16 at 19:57

1 Answers1

0

You cannot search directly through a file for a String; you must first convert to a string. Also, you are opening the file in a mode, which is write-only, so try something like this:

C1 = open("Class1.txt","r")
dataList = C1.readlines()
data = ""
for line in dataList:
    data += line
C1.close()

# do stuff with data, which is a string

Other Notes:

I do not recommend having a space in your file name; rename it to Class.txt as in my example.

To .find(something), something must be a string, like: data.find("S_N")

AMACB
  • 1,290
  • 2
  • 18
  • 26
  • There's nothing wrong with having spaces in file names. Why would there be? – MattDMo Jan 26 '16 at 20:27
  • It's more confusing when dealing with command line, but you don't have to. – AMACB Jan 26 '16 at 21:50
  • If you're on the command line, just put the file name in quotes, or escape the spaces (on POSIX systems). I just don't understand why you'd even bother telling someone to do this. We're not using DOS anymore... – MattDMo Jan 26 '16 at 21:53
  • Like I said, you don't have to... – AMACB Jan 26 '16 at 22:16